在mongodb(或java)中按数组查找对象

时间:2013-03-30 00:13:25

标签: mongodb mongodb-query mongo-java

我有一个像这样的集合(数据集):

{
  "_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"));  
} 

3 个答案:

答案 0 :(得分:4)

以下是我对自己问题的回答。我希望这对某人有帮助。

BasicDBObject query = new BasicDBObject();
    BasicDBObject field = new BasicDBObject();

    query.put("id", "Product1");
    field.put("id", 1);
    field.put("productFeature", 1);      
    field.put("_id", 0);

    DBCursor cursor = coll.find(query, field);
    while (cursor.hasNext()) {
        BasicDBObject result = (BasicDBObject) cursor.next();
        System.out.println(result);
        ArrayList<BasicDBObject> features = (ArrayList<BasicDBObject>) result.get("productFeature");

        for (BasicDBObject embedded : features) {
            String featuresId = (String) embedded.get("id");

            BasicDBObject query2 = new BasicDBObject();
            BasicDBObject field2 = new BasicDBObject();

            query2.put("id", featuresId);
            field2.put("id", 1);
            field2.put("label", 1);
            field2.put("_id", 0);
            DBCursor cursor2 = coll.find(query2, field2);

            while (cursor2.hasNext()) {
                System.out.println(cursor2.next());
            }
        }
    }

答案 1 :(得分:2)

您必须将文档结构中的“路径”提供给要从文档根目录查询的字段。在这种情况下,路径是'productFeature' - &gt; 'ID'。 MongoDB使用点(。)代替箭头,例如

db.dataset.find({ "productFeature.id" : "Product1" });

在Java中,你做了类似的事情:

BasicDBObject query = new BasicDBObject("productFeature.id" : "Product1");

DBCursor cursor = coll.find(query).sort( new BasicDBObject("label", 1));
while (cursor.hasNext()){
   System.out.println(cursor.next().get("id"));
}

答案 2 :(得分:0)

在Java中,您还可以将Query类与MongoTemplate结合使用。

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

    @Autowired
    private final MongoTemplate mongoTemplate;

...

public YourObjectClass findProduct1(){
        Query query = new Query();
        query.addCriteria(Criteria.where("productFeature.id").is("Product1"));
        List<YourObjectClass> result = this.mongoTemplate.find(query, YourObjectClass.class);
  return result;
}