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

查看:35
本文介绍了如何在 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天全站免登陆