在mongodb中查找对象数组数据字段

时间:2016-08-21 17:18:29

标签: mongodb

MongoDb数据显示如此。

{
"_id" : ObjectId("57b1fc8b6ad888063048e72e"),
"createdprojectName" : "Buffer Stock",
"createdprojectData" : [ 
        {
            "Status" : "OnAir",
            "No" : "1",
            "Site_Name" : "hjghjgjhg",
            "Site_ID" : "gjhgjhg",
            "OnAir_Target_Date" : "2016-08-01",
            "Region" : "1",
            "OnAir_Actual_Date" : "2016-08-10",
            "G2_3G_4G" : "2G"
        }, 
        {
            "Status" : "TE WIP",
            "No" : "2",
            "Site_Name" : "sadsdssd",
            "Site_ID" : "dvvs",
            "OnAir_Target_Date" : "2016-08-01",
            "Region" : "1",
            "OnAir_Actual_Date" : "",
            "G2_3G_4G" : "2G"
        }, 
        {
            "Status" : "OnAir",
            "No" : "1",
            "Site_Name" : "sadsadsdadadadsadsad",
            "OnAir_Target_Date" : "2016-07-01",
            "Site_ID" : "sadsad",
            "OnAir_Actual_Date" : "2016-07-13",
            "Region" : "1",
            "G2_3G_4G" : "3G"
        }],
"createdprojectChechByManager" : "false"
}

我想仅在数据字段上方找到Status = OnAir数据过滤。过滤上述数据后,应显示没有Status = TE WIP数据对象。我如何使用mongodb查询创建查找查询。

            BasicDBObject findQuery = new BasicDBObject();
            BasicDBObject elemMatch = new BasicDBObject();    
            DBObject statusQuery = new BasicDBObject("Status", "OnAir");   
            DBObject fields = new BasicDBObject("$elemMatch", statusQuery);
            DBObject query = new BasicDBObject("createdprojectData",fields);
            findQuery.put("createdprojectData", 1);    
            DBCursor cursor = collection.find(query, findQuery);

2 个答案:

答案 0 :(得分:0)

我尝试使用聚合。

将给定的示例插入到股票集合中,如下所示

> db.stock.insert({ "_id" : ObjectId("57b1fc8b6ad888063048e72e"), "createdprojectName" : "Buffer Stock", "createdprojectData" : [          {             "Status" : "OnAir",             "No" : "1",             "Site_Name" : "hjghjgjhg",             "Site_ID" : "gjhgjhg",             "OnAir_Target_Date" : "2016-08-01",             "Region" : "1",             "OnAir_Actual_Date" : "2016-08-10",             "G2_3G_4G" : "2G"         },          {             "Status" : "TE WIP",             "No" : "2",             "Site_Name" : "sadsdssd",             "Site_ID" : "dvvs",             "OnAir_Target_Date" : "2016-08-01",             "Region" : "1",             "OnAir_Actual_Date" : "",             "G2_3G_4G" : "2G"         },          {             "Status" : "OnAir",             "No" : "1",             "Site_Name" : "sadsadsdadadadsadsad",             "OnAir_Target_Date" : "2016-07-01",             "Site_ID" : "sadsad",             "OnAir_Actual_Date" : "2016-07-13",             "Region" : "1",             "G2_3G_4G" : "3G"         }], "createdprojectChechByManager" : "false" });

然后使用聚合来获得所需的输出

db.stock.aggregate([{"$unwind":"$createdprojectData"},{"$match":{"createdprojectData.Status":"OnAir"}},{"$group":{"_id":"$_id","createdprojectData":{"$push":"$createdprojectData"}}}]).pretty();

答案 1 :(得分:0)

这件作品对我来说很完美:

db.getCollection('Test').aggregate(
[{"$unwind":"$createdprojectData"},
{"$match":{"createdprojectData.Status" : "OnAir" }}]);