如何在Mongodb

时间:2019-01-17 06:51:37

标签: json mongodb

{      
    "_id" : ObjectId("5b98e5d17d542a2560ec026d"), 
    "RequestID" : "1536746958",     
    "JSON_Request" : [2,"1536746958","Heartbeat",{}],    
    "JSON_Response" : [3,"1536746958",{"currentTime":"2018-09-12T10:09:21.435Z"}] 
}

想在“ JSON_Request”字段中获取包含“ Heartbeat”的文档

3 个答案:

答案 0 :(得分:0)

请尝试使用此db.getCollection('request').find({JSON_Request: 'Heartbeat'}) MongoDb 4.x

或者MongoDb 3.6 db.getCollection('request').find({ JSON_Request: { $elemMatch: {$eq: 'Heartbeat'} }})也选中elemMatch MongoDb 3.6

答案 1 :(得分:0)

像这样尝试$elemMatch

collection.find({ JSON_Request: { $elemMatch: { $eq: "Heartbeat" } } })

答案 2 :(得分:0)

您可以使用MongoDB的$in运算符。官方文档的链接:Click

示例: 我在queryAns集合中有两个文档为

db.queryAns.find({}).pretty()
{
    "_id" : ObjectId("5b98e5d17d542a2560ec026d"),
    "RequestID" : "1536746958",
    "JSON_Request" : [
        2,
        "1536746958",
        "Heartbeat1",
        {

        }
    ],
    "JSON_Response" : [
        3,
        "1536746958",
        {
            "currentTime" : "2018-09-12T10:09:21.435Z"
        }
    ]
}
{
    "_id" : ObjectId("5b98e5d17d542a2560ec024d"),
    "RequestID" : "1536746958",
    "JSON_Request" : [
        2,
        "1536746958",
        "Heartbeat",
        {

        }
    ],
    "JSON_Response" : [
        3,
        "1536746958",
        {
            "currentTime" : "2018-09-12T10:09:21.435Z"
        }
    ]
}

并仅过滤掉“ JSON_Request”中包含“ Heartbeat”的结果,

  

db.queryAns.find({“ JSON_Request”:{$ in:[“ Heartbeat”]}})。pretty()

{
    "_id" : ObjectId("5b98e5d17d542a2560ec024d"),
    "RequestID" : "1536746958",
    "JSON_Request" : [
        2,
        "1536746958",
        "Heartbeat",
        {

        }
    ],
    "JSON_Response" : [
        3,
        "1536746958",
        {
            "currentTime" : "2018-09-12T10:09:21.435Z"
        }
    ]
}