mongodb 如何将查询结果(单个文档)存储到变量中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21284857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to store query result (a single document) into a variable?
提问by Robert Stam
I would like to save a single document into a variable in Mongo JS shell, and manipulate the document (read/write several attributes) for latter operations, but Mongo JS does not seem to put anything into the variable:
我想将单个文档保存到 Mongo JS shell 中的变量中,并为后面的操作操作文档(读/写多个属性),但 Mongo JS 似乎没有将任何内容放入变量中:
> a = db.col.find().limit(1)
{ "_id" : ObjectId("52dfccba5fd17fe6a4d0051a"), "a" : 16807, "b" : 475249 }
> a
>
Does mongo support the usage? Or was there a mistake?
mongo 是否支持使用?或者有什么错误?
回答by bpruitt-goddard
You need to use var
like so:
你需要var
像这样使用:
> var a = db.col.find().limit(1)
{ "_id" : ObjectId("52dfccba5fd17fe6a4d0051a"), "a" : 16807, "b" : 475249 }
> a
{ "_id" : ObjectId("52dfccba5fd17fe6a4d0051a"), "a" : 16807, "b" : 475249 }
Doing some testing I have noticed that the find()
method does appear to be setting the variable to a cursor. In these cases, you lose the variable after the next statement.
做一些测试我注意到该find()
方法似乎确实将变量设置为游标。在这些情况下,您会在下一个语句之后丢失变量。
> var a = db.col.find().limit(1)
{ "_id" : ObjectId("52dfccba5fd17fe6a4d0051a"), "a" : 16807, "b" : 475249 }
> var b = 'test'
> a
>
If you need to keep the variable around for longer, try explicitly iterating the variable before setting it using toArray()
.
如果您需要将变量保留更长时间,请尝试在使用toArray()
.
> var a = db.col.find().limit(1).toArray()
{ "_id" : ObjectId("52dfccba5fd17fe6a4d0051a"), "a" : 16807, "b" : 475249 }
> var b = 'test'
> a
[
{
"_id" : ObjectId("52dfccba5fd17fe6a4d0051a"),
"a" : 16807,
"b" : 475249
}
]
回答by Robert Stam
find returns a cursor. A cursor can only be enumerated once, so once you've enumerated a cursor it has reached the end and will yield no further documents. So in the following example the variable a is assigned a cursor value, and the first time we evaluate "a" in the shell it echoes the results of enumerating the cursor. The second time we evalute "a" it enumerates the cursor again, but this time the cursor is empty so no documents are echoed:
find 返回一个游标。一个游标只能被枚举一次,所以一旦你枚举了一个游标,它就已经到了末尾并且不会产生更多的文档。所以在下面的例子中,变量 a 被分配了一个游标值,我们第一次在 shell 中评估“a”时,它会回显枚举游标的结果。第二次我们评估“a”时,它再次枚举游标,但这次游标是空的,所以没有文件被回显:
> var a = db.test.find().limit(1)
> a
{ "_id" : ObjectId("52dfccba5fd17fe6a4d0051a"), "a" : 16807, "b" : 475249 }
> a
>
The previous example returns a cursor even though you used limit(1). It's not that "a" loses its value, it still refers to the same cursor, it's just that the cursor it refers to has reached its end.
即使您使用了 limit(1),前面的示例也会返回一个游标。不是“a”失去了它的值,它仍然指的是同一个游标,只是它所指的游标已经到了它的末尾。
If you want to read a single document and have the return value be a document instead of a cursor use findOne:
如果您想读取单个文档并且返回值是文档而不是游标,请使用 findOne:
> var a = db.test.findOne()
> a
{ "_id" : ObjectId("52dfccba5fd17fe6a4d0051a"), "a" : 16807, "b" : 475249 }
> a
{ "_id" : ObjectId("52dfccba5fd17fe6a4d0051a"), "a" : 16807, "b" : 475249 }
>
回答by fgalan
If you want only one element and want to avoid the "lose the variable" problem described in another answers you can use findOne()
instead of find()
:
如果您只需要一个元素并希望避免另一个答案中描述的“丢失变量”问题,您可以使用findOne()
代替find()
:
> var doc = db.col.findOne()
The doc
variable set in that way will be permanent.
doc
以这种方式设置的变量将是永久的。
回答by Trong Pham
If you are using aggregate
or find
to query, you had better use var
:
如果您正在使用aggregate
或find
查询,最好使用var
:
var result = db.mycollector.find()
print(result._batch[0]) // get first element