简体   繁体   English

如何使用Mongoid中的objectID查找rails的mongodb记录的各种属性?

[英]How do you look up various properties of a mongodb record using the objectID in mongoid for rails?

Using MongoID and Rails 3... 使用MongoID和Rails 3 ...

If I store the objectID as a reference to an object in my database how do I use it to query the object and return its other properties? 如果我将objectID存储为对数据库中对象的引用,如何使用它查询该对象并返回其其他属性?

Let's say my object is: 假设我的对象是:

{ 
 "_id" : ObjectId( "4da0f56ad1f4a901ba455340007" ),
 "name" : "John",
 "age" : "40" 
}

If I had the ObjectId stored in by DB - how would I use it to pull the name or age back to my view? 如果我有DB存储的ObjectId,我将如何使用它将名称或年龄拉回到我的视图? If the object is stored in another model (that doesn't belong to the view) is there anything else I need to do? 如果对象存储在另一个模型(不属于视图)中,我还有其他需要做的事情吗?

Look at database references: 查看数据库引用:

http://www.mongodb.org/display/DOCS/Database+References http://www.mongodb.org/display/DOCS/Database+参考

Otherwise if you have the object id as references stored to another object inside a different collection then your application has to know where the collection is and fetch it from there using another query. 否则,如果您将对象ID作为引用存储到另一个集合中另一个对象的引用,则您的应用程序必须知道该集合在哪里,并使用另一个查询从那里获取它。

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

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