MongoDB:查找除日期范围条件匹配的文档之外的所有文档

要求:我要收集所有did not perform the "View" event in last 5 hours客户。

数据:

{
    name: "Sheldon",
    events: [
        {
            event: "View",
            timestamp: "timestamp equivalent to 10 hours ago"
        },
        {
            event: "Some other event",
            timestamp: "timestamp equivalent to 8 hours ago"
        },
        {
            event: "View",
            timestamp: "timestamp equivalent to 2 hours ago"
        }
    ]
},
{
    name: "Leonard",
    events: [
        {
            event: "View",
            timestamp: "timestamp equivalent to 10 hours ago"
        }
    ]
},
{
    name: "Howard",
    events: [
        {
            event: "View",
            timestamp: "timestamp equivalent to 2 hours ago"
        }
    ]
},
{
    name: "Raj",
    events: [
        {
            event: "Some other event",
            timestamp: "timestamp equivalent to 6 hours ago"
        }
    ]
}

我已经尝试了以下内容,但它总是返回"Sheldon" (可能是因为其他事件最终符合标准?)。

q.where({ 
    $and: [
        {
            'events': { 
                $not: { 
                    $elemMatch: {
                        event: "View",
                        timestamp: {
                            $gte: "timestamp equivalent to 5 hours ago"
                        }
                    }
                }
            }
        }
    ]
});

我该怎么做才能返回只有"Leonard""Raj"文件?


为了让用户在过去5个小时内没有访问过。 你可以尝试一下

db.collectionName.find({
    events: { 
        $not: { 
            $elemMatch: {
                event: "View",
                timestamp: {
                    $gt: ISODate("2017-03-29T05:12:37.420Z") // equivalent to 5 hours ago
                }
            }
         }
    }
}, {name: 1})

这个查询仅为您的给定示例returnedLeonardRaj文档

NB:就像你的查询,但我用find而不是where ,不需要使用$and和使用$gt而不是$gte

测试文件。

{
    "_id" : ObjectId("58db886e4b9e731aaefa9820"),
    "name" : "Sheldon",
    "events" : [ 
        {
            "event" : "View",
            "timestamp" : ISODate("2017-03-29T00:09:18.723Z")
        }, 
        {
            "event" : "Some other event",
            "timestamp" : ISODate("2017-03-29T02:10:04.492Z")
        }, 
        {
            "event" : "View",
            "timestamp" : ISODate("2017-03-29T08:11:02.196Z")
        }
    ]
}
{
    "_id" : ObjectId("58db886e4b9e731aaefa9821"),
    "name" : "Leonard",
    "events" : [ 
        {
            "event" : "View",
            "timestamp" : ISODate("2017-03-29T00:11:23.084Z")
        }
    ]
}
{
    "_id" : ObjectId("58db886e4b9e731aaefa9822"),
    "name" : "Howard",
    "events" : [ 
        {
            "event" : "View",
            "timestamp" : ISODate("2017-03-29T08:11:02.196Z")
        }
    ]
}
{
    "_id" : ObjectId("58db886e4b9e731aaefa9823"),
    "name" : "Raj",
    "events" : [ 
        {
            "event" : "Some other event",
            "timestamp" : ISODate("2017-03-29T04:10:42.972Z")
        }
    ]
}

应用我的查询结果后:

{
    "_id" : ObjectId("58db886e4b9e731aaefa9821"),
    "name" : "Leonard"
},
{
    "_id" : ObjectId("58db886e4b9e731aaefa9823"),
    "name" : "Raj"
}

当我用你的确切数据运行你的查询时,谢尔顿文档被返回,因为当你比较字符串“2小时前”和字符串“5小时前”时,字符“2”小于字符“5”,所以文档正确返回。 字符串“10小时前”也被排序为小于“5小时前”。

但是,如果您将数据调整为具有如下所示的实际数字,则不会返回Sheldon文档。

所以你的查询对我来说很好,你只需要用日期,数字或者结构化的字符串替换时间戳,以便它们按正确的顺序排序。

修改的数据:

{
    name: "Sheldon",
    events: [
        {
            event: "View",
            timestamp: -10
        },
        {
            event: "Some other event",
            timestamp: -8
        },
        {
            event: "View",
            timestamp: -2
        }
    ]
},
{
    name: "Leonard",
    events: [
        {
            event: "View",
            timestamp: -10
        }
    ]
},
{
    name: "Howard",
    events: [
        {
            event: "View",
            timestamp: -2
        }
    ]
},
{
    name: "Raj",
    events: [
        {
            event: "Some other event",
            timestamp: -6
        }
    ]
}

修改的查询:

{ 
    $and: [
        {
            'events': { 
                $not: { 
                    $elemMatch: {
                        event: "View",
                        timestamp: {
                            $gte: -5
                        }
                    }
                }
            }
        }
    ]
}

结果:

{ "name" : "Leonard", "events" : [ { "event" : "View", "timestamp" : -10 } ] }
{ "name" : "Raj", "events" : [ { "event" : "Some other event", "timestamp" : -6 } ] }
链接地址: http://www.djcxy.com/p/38823.html

上一篇: MongoDB: Find all documents except those where a date range condition matches

下一篇: SKErrorDomain Code=0 in In