如何为对象编写mongo查询

时间:2017-04-12 08:46:11

标签: mongodb criteria-api mongotemplate

{
    "_id" : "WR10005",
    "_class" : "com.bioraid.mes.model.WorkOrders",
    "rountingNumber" : "R006",
    "orderId" : "MR-1017",
    "consumables" : "Chip",
    "workOrderStatus" : "pending",
    "deliveryStatus" : "on time",
    "lastCompletedStage" : "testProces",
    "dateAssigned" : ISODate("2017-02-17T05:38:57.631Z"),
    "dateCompleted" : ISODate("2017-02-17T07:53:32.680Z"),
    "opretorProcessStatus" : [ 
        {
            "opretorid" : "USER114",
            "process" : "testProces",
            "status" : "Done",
            "workCenterId" : "WC1",
            "startDate" : ISODate("2017-02-17T06:30:16.813Z"),
            "endDate" : ISODate("2017-02-17T06:42:23.237Z"),
            "qualityManagerReview" : {
                "qaId" : "",
                "status" : "Done",
                "note" : ""
            }
        }, 
        {
            "opretorid" : "USER116",
            "process" : "testProces",
            "status" : "Done",
            "workCenterId" : "WC1",
            "startDate" : ISODate("2017-02-17T06:30:16.813Z"),
            "endDate" : ISODate("2017-02-17T06:42:23.237Z")
        }
    ],
    "workOrdersRouting" : {
        "testProces" : [ 
            {
                "routingStep" : "R006*010",
                "operationName" : "Operation1",
                "operationStatus" : "Completed",
                "operationResult" : "Pass",
                "operationNote" : "test"
            }, 
            {
                "routingStep" : "R006*020",
                "operationName" : "adalks",
                "operationStatus" : "Completed",
                "operationResult" : "Pass",
                "operationNote" : "sddsdfsf"
            }
        ],
        "Embossing" : [ 
            {
                "routingStep" : "R006*030",
                "operationName" : "Water Cleaning",
                "operationStatus" : "",
                "operationResult" : "",
                "operationNote" : ""
            }
        ],
        "Turbidity" : [ 
            {
                "routingStep" : "R006*040",
                "operationName" : "Embossing Assembly",
                "operationStatus" : "",
                "operationResult" : "",
                "operationNote" : ""
            }
        ]
    }
}

我想得到{'opretorProcessStatus.qualityManagerReview.status' : {$ne : null}},但这并没有给出正确的结果,任何人都可以帮我写这篇文章。

,另一个是db.getCollection('workOrders').find({'opretorProcessStatus.qualityManagerReview.status' : "Done"})

1 个答案:

答案 0 :(得分:0)

您可以使用$ elemMatch来获取所需的文档。

要使用opretorProcessStatus.qualityManagerReview.status = Done查找文档,请使用此查询:

    db.getCollection("workOrders").find({"opretorProcessStatus":{"$elemMatch":{""qualityManagerReview.status":"Done"}}})

要查找没有状态为null的文档:

    db.getCollection("workOrders").find({"opretorProcessStatus":{"$elemMatch":{""qualityManagerReview.status":{"$ne":null}}}})