簡體   English   中英

使用mongodb在數組中查找元素

[英]Find element in array using mongodb

我只是想基於查詢來計算數組中的元素。 我嘗試了以下命令,但沒有解決我的問題。

我想計算其時間戳在DATA2數組的“ 2017-02-17T18:30:00.000Z和” 2017-02-18T18:29:59.999Z”之間的元素,但它僅返回1。

執行的代碼:

代碼1

db.ABC.aggregate([{
                $match: {
                    $and: [{

                        DATA2: {
                            $exists: true
                        }

                    }, {
                        "DATA2.TimeStamp": {
                            $gte: require('../../modules/getDates').getFromDate(item),
                            $lte: require('../../modules/getDates').getToDate(item)
                        }
                    }, {
                        Client_id: "123" /*req.query.client_id*/
                    }]
                }
            }, {


                $project: {

                    DATASiz: {
                        $size: "$DATA2"
                    },
                    "has bananas": {
                        $in: ["DATA2.$.TimeStamp"]
                    }
                }
            }], function(err, result) {

                console.log(result)
                callBack();

            })

代碼2

db.abc.find({ $and:[{DATA2: {$exists: true}},{Client_id: "123"},{"DATA2": { $elemMatch: { TimeStamp: {  $gte: require('../../modules/getDates').getFromDate(item), $lte: require('../../modules/getDates').getToDate(item) } } }}]
 }, function(err, result) {

             console.log(JSON.stringify(result))
             callBack();

           })

代碼3

//db.abc.find //also tried
    db.abc.count({
                    $and: [{

                        DATA2: {
                            $exists: true
                        }

                    }, {
                        "DATA2.TimeStamp": {
                            $gte: require('../../modules/getDates').getFromDate(item),
                            $lte: require('../../modules/getDates').getToDate(item)
                        }
                    }, {
                        Client_id: "123" /*req.query.client_id*/
                    }]
                },{
                    "DATA2.$":1
                }, function(err, result) {

                    console.log(result)
                    callBack();

                })

JSON格式:

{
    "_id": {
        "$oid": "57c7404985737e2c78fde6b3"
    },
    "ABC": "1304258470",
    "Status": "Not Found",
    "DATA1": [
        {123},{123},{123}
    ],
    "Remark": "Not Found",
    "DATA2": [
        {
            "TimeStamp": "2017-02-18T09:01:43.060Z",
            "NdrStatus": "Door Locked",

        },
        {
            "TimeStamp": "2017-02-18T08:09:43.347Z",
            "NdrStatus": "HOLD",

        },
        {
            "TimeStamp": "2017-02-20T08:09:43.347Z",
            "NdrStatus": "HOLD",

        }
    ]
}

結果:我正在使用CODE 3獲取DATA2的第一個元素,但我知道根據查詢,將返回2個元素。

我希望計數為2。 還用了$ unwind $ redact預先感謝。

您可以為此使用$filter$size運算符:

var start = require('../../modules/getDates').getFromDate(item),
    end   = require('../../modules/getDates').getToDate(item);

db.ABC.aggregate([
    {
        "$match": {
            "DATA2": { "$exists": true },
            "DATA2.TimeStamp": { "$gte": start, "$lte": end },
            "Client_id": "123"
        }
    },
    {
        "$project": {
            "DATASiz": {
                "$size": {
                    "$filter": {
                        "input": "$DATA2",
                        "as": "item",
                        "cond": {
                            "$and": [
                                { "$gte": ["$$item.TimeStamp", start] },
                                { "$lte": ["$$item.TimeStamp", end] }
                            ]
                        }
                    }
                }
            }
        }
    }
], function(err, result) {
    console.log(result);
    callBack();
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM