2

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

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

3 回答 3

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());
            }
        }
    }
于 2013-04-07T21:15:53.007 回答
2

您必须将文档结构中的“路径”提供给要从文档根目录查询的字段。在这种情况下,路径是“productFeature”-->“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"));
}
于 2013-03-30T00:22:37.527 回答
0

In Java you could also use the Query class in combination with 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;
}
于 2020-10-22T15:44:57.067 回答