我试图updates
在两个约会之间。因此,我有一些硬编码的条件。(请参见下面的示例)
更新集合:
{
thread_id: 'Number',
text: 'String',
timestamp: 'Number',
}
问题是我只想对所有条件进行一次查询,但是我不知道该怎么做,我也不知道它是否可以实现。
例:
// Records in the updates collection
const updates = [
{ thread_id: 1, text: 'Hello', date: 2000 },
{ thread_id: 2, text: 'GoodBye!', date: 1999 },
{ thread_id: 2, text: 'This is a test', date: 3000 },
{ thread_id: 3, text: 'We are happy!', date: 6000 }
];
// Conditions (Hard-Coded)
const conditions = [
{
thread_id: 1,
date_start: 1899,
date_end: 2059
}, {
thread_id: 2,
date_start: 1905,
date_end: 2000
}, {
thread_id: 3,
date_start: 1905,
date_end: null // means no limitation
}
];
// Results:
const results = [
{ thread_id: 1, text: 'Hello', timestamp: 2000 },
{ thread_id: 2, text: 'GoodBye!', timestamp: 1999 },
{ thread_id: 3, text: 'We are happy!', timestamp: 6000 },
]
// Query (Pseudoquery)
update.thread_id = condition.thread_id
condition.date_start <= update.timestamp <= condition.date_end
如您所见,由于该属性未在条件中设置,因此This is a test
被排除并We are happy!
存在end
。
也许您可以如下更改一些困难的条件:
mongos> const cond= [ {thread_id:1 ,$or:[ {date:{$lte:1899}} ,{date:{$gte:2059}}] },{thread_id:2 ,$or:[ {date:{$gte:2000}} ,{date:{$lte:1905} } ] } , {thread_id:3 , $or:[ {date:{$lte:1905}}, {date:{$gte:9999}}]} ]
mongos> db.updates.find({$or:cond})
{ "_id" : ObjectId("5ff9b208da146da4e1359dcf"), "thread_id" : 2, "text" : "This is a test", "date" : 3000 }
mongos> db.updates.find()
{ "_id" : ObjectId("5ff9b208da146da4e1359dcf"), "thread_id" : 2, "text" : "This is a test", "date" : 3000 }
{ "_id" : ObjectId("5ff9b399da146da4e1359dd1"), "thread_id" : 1, "text" : "Hello", "date" : 2000 }
{ "_id" : ObjectId("5ff9b3aada146da4e1359dd2"), "thread_id" : 2, "text" : "GoodBye!", "date" : 1999 }
{ "_id" : ObjectId("5ff9b3c6da146da4e1359dd3"), "thread_id" : 3, "text" : "We are happy!", "date" : 6000 }
mongos> db.updates.find({$or:cond})
{ "_id" : ObjectId("5ff9b208da146da4e1359dcf"), "thread_id" : 2, "text" : "This is a test", "date" : 3000 }
mongos> db.updates.deleteMany({$or:cond})
{ "acknowledged" : true, "deletedCount" : 1 }
mongos> db.updates.find()
{ "_id" : ObjectId("5ff9b399da146da4e1359dd1"), "thread_id" : 1, "text" : "Hello", "date" : 2000 }
{ "_id" : ObjectId("5ff9b3aada146da4e1359dd2"), "thread_id" : 2, "text" : "GoodBye!", "date" : 1999 }
{ "_id" : ObjectId("5ff9b3c6da146da4e1359dd3"), "thread_id" : 3, "text" : "We are happy!", "date" : 6000 }
mongos>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句