Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

二、Mongodb实战之——Mongodb Shell实现简单插入和查询

$
0
0

  1. insert

    source code

    function (obj, _allow_dot) {
        if (!obj) {
            throw "no object passed to insert!";
        }
        if (!_allow_dot) {
            this._validateForStorage(obj);
        }
        if (typeof obj._id == "undefined" && !Array.isArray(obj)) {
            var tmp = obj;
            obj = {_id:new ObjectId};
            for (var key in tmp) {
                obj[key] = tmp[key];
            }
        }
        this._db._initExtraInfo();
        this._mongo.insert(this._fullName, obj);
        this._lastID = obj._id;
        this._db._getExtraInfo("Inserted");
    }
    

    example:

    db.users.insert({"userName":"chjzh","pwd":"123"})

  2. find

    source code

    function (query, fields, limit, skip, batchSize, options) {
        return new DBQuery(this._mongo, this._db, this, this._fullName, this._massageObject(query), fields, limit, skip, batchSize, options || this.getQueryOptions());
    }
    

    example

    db.users.find({"userName":"chjzh"})

  3. save

    source code

    function (obj) {
        if (obj == null || typeof obj == "undefined") {
            throw "can't save a null";
        }
        if (typeof obj == "number" || typeof obj == "string") {
            throw "can't save a number or string";
        }
        if (typeof obj._id == "undefined") {
            obj._id = new ObjectId;
            return this.insert(obj);
        } else {
            return this.update({_id:obj._id}, obj, true);
        }
    }
    

    example

    db.users.save({"userName":"chjzh","pwd":"111111"})

  4. count

    source code

    function (x) {
        return this.find(x).count();
    }
    

    example

    db.users.count()
    

  5. find

    source code

    function (query, fields, limit, skip, batchSize, options) {
        return new DBQuery(this._mongo, this._db, this, this._fullName, this._massageObject(query), fields, limit, skip, batchSize, options || this.getQueryOptions());
    }
    

    example

    db.users.find()
    db.users.find({"userName":"chjzh"})
    
作者:bychjzh 发表于2013-3-15 19:19:19 原文链接
阅读:34 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles