根据硬编码条件过滤数据(MongoDB)

侯赛因沙士

我试图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

R2D2

也许您可以如下更改一些困难的条件:

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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

根据条件过滤数据

来自分类Dev

根据特定条件过滤数据

来自分类Dev

根据某些条件从模型中过滤数据

来自分类Dev

根据等级和条件过滤数据

来自分类Dev

python,根据几种条件过滤数据框

来自分类Dev

根据过滤条件提取原始数据

来自分类Dev

根据分组条件过滤数据集

来自分类Dev

根据条件熊猫过滤数据框行

来自分类Dev

根据条件在角度选择中过滤数据

来自分类Dev

在何处根据多种条件过滤数据

来自分类Dev

Python 数据框:根据 groupby 条件过滤数据框

来自分类Dev

循环一些硬编码的数据

来自分类Dev

来自数据库的硬编码变量

来自分类Dev

Jsoup问题抓取非硬编码的数据

来自分类Dev

来自数据库的硬编码变量

来自分类Dev

根据条件过滤id

来自分类Dev

熊猫:使用〜mask根据多个条件从数据中过滤行

来自分类Dev

根据条件过滤熊猫数据框-在NaN值上失败

来自分类Dev

R如何根据后续行中的条件过滤数据帧?

来自分类Dev

根据条件熊猫过滤数据帧(字符串拆分)

来自分类Dev

根据特定列上的条件计数过滤数据框

来自分类Dev

根据多个条件按列名过滤数据框

来自分类Dev

根据另一列的条件过滤数据框

来自分类Dev

根据熊猫数据框中的某些条件过滤行

来自分类Dev

根据熊猫中的某些条件过滤数据框的行

来自分类Dev

如何根据条件terraform过滤来自本地的数据?

来自分类Dev

根据列中的条件过滤数据表

来自分类Dev

熊猫:使用〜mask根据多个条件从数据中过滤行

来自分类Dev

如何根据某些过滤条件从bigquery导出数据