Mongo REPL存储结果为var?

时间:2011-12-19 16:11:30

标签: mongodb read-eval-print-loop

好奇 - 在下面的例子中,为什么mongo REPL不会将用户哈希存储在第一次打印之后的变量'a'中?

是否与mongo的懒惰查询评估有关?

> var a = db.users.find(0)
> a
{ "_id" : ObjectId("4eed6dc299cd67e275000001"), "provider" : "facebook", "uid" : "343323487", "name" : "Brian Jordan", "email" : "redacted@redacted.com" }
> a
>

1 个答案:

答案 0 :(得分:2)

您可以使用findOne

> var a = db.testcoll.findOne()
> a
{
    "_id" : ObjectId("4e7930a3ff647405d6000003"),
    "bf" : false,
    "df" : ISODate("2011-09-21T00:32:35.629Z")
}

或者您可以查看其来源并执行类似的操作

> db.testcoll.findOne
function (query, fields) {
    var cursor = this._mongo.find(this._fullName, this._massageObject(query) || {}, fields, -1, 0, 0, 0);
    if (!cursor.hasNext()) {
        return null;
    }
    var ret = cursor.next();
    if (cursor.hasNext()) {
        throw "findOne has more than 1 result!";
    }
    if (ret.$err) {
        throw "error " + tojson(ret);
    }
    return ret;
}