mongodb 如何在 mongo 控制台中通过 ObjectId 搜索对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8233014/
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 do I search for an object by its ObjectId in the mongo console?
提问by jcollum
I've found this question answered for C# and Perl, but not in the native interface. I thought this would work:
我发现这个问题在 C# 和 Perl 中得到了回答,但在本机界面中没有。我认为这会奏效:
db.theColl.find( { _id: ObjectId("4ecbe7f9e8c1c9092c000027") } )
db.theColl.find( { _id: ObjectId("4ecbe7f9e8c1c9092c000027") } )
The query returned no results. I found the 4ecbe7f9e8c1c9092c000027 by doing db.theColl.find()
and grabbing an ObjectId. There are several thousand objects in that collection.
查询未返回任何结果。我通过执行db.theColl.find()
并抓取 ObjectId找到了 4ecbe7f9e8c1c9092c000027 。该集合中有数千个对象。
I've read all the pages that I could find on the mongodb.org website and didn't find it. Is this just a strange thing to do? It seems pretty normal to me.
我已经阅读了可以在 mongodb.org 网站上找到的所有页面,但没有找到。这只是一件奇怪的事情吗?对我来说似乎很正常。
回答by Tyler Brock
Not strange at all, people do this all the time. Make sure the collection name is correct (case matters) and that the ObjectId is exact.
一点也不奇怪,人们一直这样做。确保集合名称正确(大小写很重要)并且 ObjectId 是准确的。
Documentation is here
文档在这里
> db.test.insert({x: 1})
> db.test.find() // no criteria
{ "_id" : ObjectId("4ecc05e55dd98a436ddcc47c"), "x" : 1 }
> db.test.find({"_id" : ObjectId("4ecc05e55dd98a436ddcc47c")}) // explicit
{ "_id" : ObjectId("4ecc05e55dd98a436ddcc47c"), "x" : 1 }
> db.test.find(ObjectId("4ecc05e55dd98a436ddcc47c")) // shortcut
{ "_id" : ObjectId("4ecc05e55dd98a436ddcc47c"), "x" : 1 }
回答by Mustafa Deniz
If you're using Node.js:
如果您使用的是 Node.js:
> var ObjectId = require('mongodb').ObjectId;
> var id = req.params.gonderi_id;
> var o_id = new ObjectId(id);
> db.test.find({_id:o_id})
Edit: corrected to new ObjectId(id), not new ObjectID(id)
编辑:更正为新的 ObjectId(id),而不是新的 ObjectID(id)
回答by MPlanchard
Even easier, especially with tab completion:
更容易,尤其是制表符补全:
db.test.find(ObjectId('4ecc05e55dd98a436ddcc47c'))
Edit: also works with the findOne
command for prettier output.
编辑:也适用findOne
于更漂亮输出的命令。
回答by Mohamed Abdullah J
You Have missed to insert Double Quotes. The Exact Query is
您错过了插入双引号。确切的查询是
db.theColl.find( { "_id": ObjectId("4ecbe7f9e8c1c9092c000027") } )
回答by saurabh gupta
If you are working on the mongo shell, Please refer this : Answer from Tyler Brock
如果您正在使用 mongo shell,请参考:来自 Tyler Brock 的回答
I wrote the answer if you are using mongodb using node.js
如果您使用 node.js 使用 mongodb,我写了答案
You don't need to convert the id into an ObjectId
. Just use :
您不需要将 id 转换为ObjectId
. 只需使用:
db.collection.findById('4ecbe7f9e8c1c9092c000027');
this collection method will automatically convert id into ObjectId.
这个集合方法会自动将id转换成ObjectId。
On the other hand :
另一方面 :
db.collection.findOne({"_id":'4ecbe7f9e8c1c9092c000027'})
doesn't work as expected. You've manually convert id into ObjectId
.
db.collection.findOne({"_id":'4ecbe7f9e8c1c9092c000027'})
没有按预期工作。您已手动将 id 转换为ObjectId
.
That can be done like this :
可以这样做:
let id = '58c85d1b7932a14c7a0a320d';
let o_id = new ObjectId(id); // id as a string is passed
db.collection.findOne({"_id":o_id});
回答by Patrick Graham
I just had this issue and was doing exactly as was documented and it still was not working.
我刚刚遇到了这个问题,并且完全按照文档中的说明进行操作,但它仍然无法正常工作。
Look at your error message and make sure you do not have any special characters copied in. I was getting the error
查看您的错误消息并确保您没有复制任何特殊字符。我收到错误消息
SyntaxError: illegal character @(shell):1:43
When I went to character 43 it was just the start of my object ID, after the open quotes, exactly as I pasted it in. I put my cursor there and hit backspace nothing appeared to happen when it should have removed the open quote. I hit backspace again and it removed the open quote, then I put the quote back in and executed the query and it worked, despite looking exactly the same.
当我转到字符 43 时,它只是我的对象 ID 的开始,在开引号之后,与我粘贴的完全一样。我将光标放在那里并按退格键,但它应该删除开引号时似乎没有发生任何事情。我再次按下退格键并删除了开放引号,然后我将引号放回并执行查询并且它工作,尽管看起来完全相同。
I was doing development in WebMatrix and copied the object id from the console. Whenever you copy from the console in WebMatrix you're likely to pick up some invisible characters that will cause errors.
我在 WebMatrix 中进行开发并从控制台复制了对象 ID。每当您从 WebMatrix 中的控制台复制时,您很可能会拾取一些会导致错误的不可见字符。
回答by Oliver Wolf
Once you opened the mongo CLI, connected and authorized on the right database.
打开 mongo CLI 后,在正确的数据库上进行连接和授权。
The following example shows how to find the documentwith the _id=568c28fffc4be30d44d0398e from a collection called “products”:
db.products.find({"_id": ObjectId("568c28fffc4be30d44d0398e")})
回答by kushal.8
回答by mina_anwer
I think you better write something like this:
我认为你最好写这样的东西:
db.getCollection('Blog').find({"_id":ObjectId("58f6724e97990e9de4f17c23")})
回答by Miguel Peguero
To use Objectid method you don't need to import it. It is already on the mongodb object.
要使用 Objectid 方法,您不需要导入它。它已经在 mongodb 对象上。
var ObjectId = new db.ObjectId('58c85d1b7932a14c7a0a320d');
db.yourCollection.findOne({ _id: ObjectId }, function (err, info) {
console.log(info)
});