如何在mongo控制台中按其ObjectId搜索对象? [英] How do I search for an object by its ObjectId in the mongo console?

查看:123
本文介绍了如何在mongo控制台中按其ObjectId搜索对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现C#和Perl回答了这个问题,但是在本机界面中却没有.我认为这可以解决问题:

I've found this question answered for C# and Perl, but not in the native interface. I thought this would work:

db.theColl.find( { _id: ObjectId("4ecbe7f9e8c1c9092c000027") } )

查询未返回任何结果.我通过执行db.theColl.find()并获取一个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.

我已经阅读了在mongodb.org网站上可以找到的所有页面,但是没有找到.这是一件奇怪的事吗?对我来说似乎很正常.

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.

推荐答案

一点都不奇怪,人们一直在这样做.确保集合名称正确(区分大小写),并且ObjectId正确.

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.

文档位于此处

> 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 }

这篇关于如何在mongo控制台中按其ObjectId搜索对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆