我有一个这样的集合(数据集):
{
"_id" : ObjectId("515611c1c6e3718ee42a5655"),
"id": "Product1",
"type": "ProductType4"
"productFeature": [
{
"id": "ProductFeature1"
},
{
"id": "ProductFeature2"
},
{
"id": "ProductFeature3"
}
]
"productPropertyNumeric": 25
},
... and more product objects...
{
"_id" : ObjectId("515611c1c6e3718ee42a5666"),
"id": "ProductFeature1",
"label": "blablabla"
},
{
"_id" : ObjectId("515611c1c6e3718ee42a5667"),
"id": "ProductFeature2",
"label": "blebleble"
},
{
"_id" : ObjectId("515611c1c6e3718ee42a5668"),
"id": "ProductFeature3",
"label": "blublublu"
} ... and more feature objects...
根据 Product1,我必须在其“productFeature”数组中找到特定产品的功能和标签。
我曾尝试在 Mongo shell 中找到它们(例如,使用变量):
var aaa = db.dataset.find({ id: "Product1" })
db.dataset.find({ id: "aaa.productFeature.id" })
但它不起作用。如果有人知道如何按数组查找对象,请帮助我。非常感谢。
PS:最好在 Java 中使用 - 例如,我应用了一个查询:
BasicDBObject query = new BasicDBObject();
query.put("type","ProductType4");
query.put("productPropertyNumeric", new BasicDBObject("$gt", 10));
DBCursor cursor = coll.find(query).sort( new BasicDBObject("label", 1));
while (cursor.hasNext()){
System.out.println(cursor.next().get("id"));
}