MongoDB:检索集合中的第一个文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16866977/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:15:07  来源:igfitidea点击:

MongoDB: Retrieving the first document in a collection

mongodb

提问by dhulihan

I'm new to Mongo, and I'm trying to retrieve the first document from a find()query:

我是 Mongo 的新手,我正在尝试从find()查询中检索第一个文档:

> db.scores.save({a: 99});
> var collection = db.scores.find();
[ 
  {   "a" : 99,   "_id" : {   "$oid" : "51a91ff3cc93742c1607ce28"   }   }
]
> var document = collection[0];
JS Error: result is undefined

This is a little weird, since a collection looks a lot like an array. I'm aware of retrieving a single document using findOne(), but is it possible to pull one out of a collection?

这有点奇怪,因为集合看起来很像数组。我知道使用 检索单个文档findOne(),但是否可以从集合中提取一个文档?

采纳答案by chaliasos

The findmethod returns a cursor. This works like an iterator in the result set. If you have too many results and try to display them all in the screen, the shell will display only the first 20 and the cursor will now point to the 20th result of the result set. If you type itthe next 20 results will be displayed and so on.

find方法返回一个游标。这就像结果集中的迭代器一样。如果你有太多的结果并试图在屏幕上显示它们,shell 将只显示前 20 个,光标现在将指向结果集的第 20 个结果。如果您键入it,将显示接下来的 20 个结果,依此类推。

In your example I think that you have hidden from us one line in the shell.

在您的示例中,我认为您在 shell 中向我们隐藏了一行。

This command

这个命令

> var collection = db.scores.find();

will just assign the result to the collectionvariable and will not print anything in the screen. So, that makes me believe that you have also run:

只会将结果分配给collection变量,不会在屏幕上打印任何内容。所以,这让我相信你也跑了:

> collection

Now, what is really happening. If you indeed have used the above command to display the content of the collection, then the cursor will have reached the end of the result set (since you have only one document in your collection) and it will automatically close. That's why you get back the error.

现在,真正发生了什么。如果您确实使用了上述命令来显示 的内容collection,那么光标将到达结果集的末尾(因为您的集合中只有一个文档)并且它会自动关闭。这就是您返回错误的原因。

There is nothing wrong with your syntax.You can use it any time you want. Just make sure that your cursor is still open and has results. You can use the collection.hasNext()method for that.

您的语法没有任何问题。您可以随时使用它。只需确保您的光标仍处于打开状态并有结果。您可以使用该collection.hasNext()方法。

回答by paulmelnikow

Is that the Mongo shell? What version? When I try the commands you type, I don't get any extra output:

那是Mongo外壳吗?什么版本?当我尝试你输入的命令时,我没有得到任何额外的输出:

MongoDB shell version: 2.4.3
connecting to: test
> db.scores.save({a: 99});
> var collection = db.scores.find();
> var document = collection[0];

In the Mongo shell, find()returns a cursor, not an array. In the docs you can see the methods you can call on a cursor.

在 Mongo shell 中,find()返回一个游标,而不是一个数组。在文档中,您可以看到可以在 cursor 上调用方法

findOne()returns a single document and should work for what you're trying to accomplish.

findOne()返回单个文档,应该为您要完成的工作而工作。

回答by Mike Gostintsev

So you can have several options.

因此,您可以有多种选择。

Using Java as the language, but one option is to get a db cursor and iterate over the elements that are returned. Or just simply grab the first one and run.

使用 Java 作为语言,但一种选择是获取 db 游标并迭代返回的元素。或者只是简单地抓住第一个并运行。

DBCursor cursor = db.getCollection(COLLECTION_NAME).find();
List<DOCUMENT_TYPE> retVal = new ArrayList<DOCUMENT_TYPE>(cursor.count());
while (cursor.hasNext()) {
    retVal.add(cursor.next());
}
return retVal;

If you're looking for a particular object within the document, you can write a query and search all the documents for it. You can use the findOne method or simply find and get a list of objects matching your query. See below:

如果要在文档中查找特定对象,则可以编写查询并搜索所有文档。您可以使用 findOne 方法或简单地查找并获取与您的查询匹配的对象列表。见下文:

DBObject query = new BasicDBObject();
query.put(SOME_ID, ID);
DBObject result = db.getCollection(COLLECTION_NAME).findOne(query) // for a single object
DBCursor cursor = db.getCollection(COLLECTION_NAME).find(query) // for a cursor of multiple objects