简体   繁体   English

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

[英]MongoDB: Retrieving the first document in a collection

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()检索单个文档,但是可以从集合中提取一个吗?

The find method returns a cursor. find方法返回一个游标。 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. 如果您有太多结果并尝试在屏幕上全部显示它们,则shell将仅显示前20个,并且光标现在将指向结果集的第20个结果。 If you type it the next 20 results will be displayed and so on. 如果您键入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 collection variable and will not print anything in the screen. 只会将结果分配给collection变量,不会在屏幕上打印任何内容。 So, that makes me believe that you have also run: 所以,这让我相信你也跑了:

> 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. 如果您确实使用了上述命令来显示collection的内容,那么光标将到达结果集的末尾(因为您的集合中只有一个文档)并且它将自动关闭。 That's why you get back the error. 这就是你找回错误的原因。

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()方法。

Is that the Mongo shell? 那是Mongo的外壳吗? What version? 什么版本? When I try the commands you type, I don't get any extra output: 当我尝试你输入的命令时,我没有得到任何额外的输出:

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. 在Mongo shell中, find()返回一个游标,而不是一个数组。 In the docs you can see the methods you can call on a cursor . 在文档中,您可以看到可以在游标上调用方法

findOne() returns a single document and should work for what you're trying to accomplish. findOne()返回一个文档,应该适用于您要完成的任务。

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. 使用Java作为语言,但一种选择是获取db游标并迭代返回的元素。 Or just simply grab the first one and run. 或者只是抓住第一个并运行。

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. 您可以使用findOne方法或只是查找并获取与查询匹配的对象列表。 See below: 见下文:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM