0

我想从数据库中获取 2n 个值。在下面的示例中,我需要在 mongodb 数据库集合中增加 2 个点的值

{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-03-01T08:00:00Z") }
{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-03-01T09:00:00Z") }
{ "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-03-15T09:00:00Z") }
{ "_id" : 4, "item" : "xyz", "price" : 5, "quantity" : 20, "date" : ISODate("2014-04-04T11:21:39.736Z") }
{ "_id" : 5, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-04-04T21:23:13.331Z") }

我期待的输出是这样的

 { "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-03-01T09:00:00Z") }
{ "_id" : 4, "item" : "xyz", "price" : 5, "quantity" : 20, "date" : ISODate("2014-04-04T11:21:39.736Z") }
..............
4

1 回答 1

1

您可以在查询中使用$mod(模)运算符find

db.collection.find({ _id: { $mod: [2, 0]} })

或者(如您的问题标题中所述)aggregate查询:

db.collection.aggregate([{ $match: { _id: { $mod: [2, 0] } } }])
于 2018-03-07T14:22:05.833 回答