如何通过_id检索文档? [英] How can I retrieve a document by _id?
本文介绍了如何通过_id检索文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我有对象ID时,我正在尝试检索文档-但是,该查询无法正常工作.
I'm trying to retrieve a document when I have an object id - however, the query does not work.
@collection = @db.collection('Mylist')
@result = @collection.find({"_id" => params[:id]})
我尝试过查询的变体-它总是产生空的-但是,当我尝试对如下所示的集合进行查询时,那会起作用.
I've tried variations of the query - it always yields empty - however when I try a query on the collection such as below, that would work.
@result = @collection.find({"Exist" => "True"})
为什么?奇怪的是,复杂的查询有效,但是通过_id进行的简单查询却什么也没有返回.
Why? It is strange that complex queries work but a simple query by _id returns nothing.
如果可能的话,我不想使用MongoMapper.
If possible, I don't want to use MongoMapper.
谢谢
推荐答案
找到它-您需要像这样包装它-
Found it - you need to wrap it like this -
find({"_id" => Mongo::ObjectId(params[:id])})
这篇关于如何通过_id检索文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文