English 中文(简体)
如何得到一个孩子 蒙戈收藏的钥匙?
原标题:How to get a child of a mongo collections by the key?

我有一个收藏用户 看起来像这个:

{
_id: myid,
name :  blabla ,
ia : {
       [0]->  an id ,
       [1]->  a second id 
      }
}

我想只有我的第一个IAID, 所以我尝试了类似的东西:

User.find({ _id: id, ia :{ key : indexia} }, [ ia ]).populate( ia ).run(rendu);

ID= Myid 和 indexia= 0 的位置 。

如果我不把那个部分: "是的:{关键: india}"我得到我所有的iA... 但我只想要一个。

Hope i was clear. Thanks

最佳回答

Thanks for your help It worker like that:

User.find({ _id: id }).only( ia ).slice( ia , indexia, 1).populate( ia ).run(rendu);

唯一的不工作,但没什么大不了的。

问题回答

MungoDB find () 命令旨在匹配整个文档 。

当您运行以下查询时

{ _id: id, ia :{ key : indexia} }

... 意思是 等同 id ia 等同对象 > 。 这可能是 < strong > not

如果我不把那个部分: "是的:{关键: india}"我得到我所有的iA... 但我只想要一个。

听上去您想要返回给定文档中的一块 。 此文档的文档是 < a href=" http://www. mongodb. org/ display/ DOCS/ Retregive+a+Subset+of+Friels" rel="nofollow" > here。 您的情况是:

find({ _id: id}, {  ia.0  : 1 })

查找的第一部分将找到文档。 第二部分将返回特定的字段 。

您收藏的图案似乎有:

{ "_id" : "id",
  "ia"  : ["id1","id2","id3"],
   etc.
}

在此情况下, 获取“ ia” 字段第一个“ id” 的查询是 :

find({}, { "ia" : {$slice:1  } })

You can read more about $slice operator here. Mongoose documents its support for the $slice operator on the query page. Hope this helps!





相关问题
How to remove a lua table entry by its key?

I have a lua table that I use as a hashmap, ie with string keys : local map = { foo = 1, bar = 2 } I would like to "pop" an element of this table identified by its key. There is a table.remove() ...

Detect if any key is pressed in C# (not A, B, but any)

[EDIT 3] I kind of "solved it" by at using the "strange" version. At least for the most important keys. It is suffient for my case, where I want to check that ALT and ALT+A are not ...

How to get key value

My sample JSON string is as below: {"8776337":{"text":"Test Message","status":"d","created_time":"1244475327","reply_number":"447624800500","completed_time":"1244475373","credits_cost":"0.4"}} "...

Hibernate map collection with constant key

I m trying to map a collection (of type map) using a foreign key and a fixed value as the key/mapping arguments. I have several tables of product types and a language table which holds stuff like ...

Changing the value in a map in Groovy

This is about a very basic program I m writing in Groovy. I have defined a map inside a method: def addItem() { print("Enter the item name: ") def itemName = reader.readLine() print(...

SWT Cross-Platform Enter Detection

I have a global filter (Display.addFilter) in SWT in which I want to detect Enter key. In Windows, pressing Enter generates SWT.CR in keyCode part of KeyListener event. Is this assumption safe for ...

热门标签