好奇 - 在下面的例子中,为什么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
>
答案 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;
}