MongoDB-字段类型阵列上的查询基础
我在MongoDB中有阵列查询有问题 I have collection with documents format like so:
{
'data':
[
{ 'itv': [0, 1], 'name': 'a' },
{ 'itv': [6, 8], 'name': 'b' },
...
]
}
I want to query documents that data have at least one element that matches the condition itv[1]-itv[0] > n
I am having a problem with array query in mongoDB
I have collection with documents format like so:
{
'data':
[
{ 'itv': [0, 1], 'name': 'a' },
{ 'itv': [6, 8], 'name': 'b' },
...
]
}
I want to query documents that data have at least one element that matches the condition itv[1]-itv[0] > n
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在聚合管道中对此进行计算,也可以使用查询语言使用
$ expr
,例如:mongo游乐场
我的个人建议是预先计算差异,这意味着新的模式看起来会有些东西像:
现在您可以使用一个简单查找查询查询此问题:
要追溯更新您的集合,您可以使用此更新:
游乐场
You can calculate this either in the aggregation pipeline or using
$expr
with the query language, like so:Mongo Playground
My personal recommendation will be to pre-calculate the difference, this means the new schema will look a little something like:
Now you can query this with a simple find query:
To retroactively update your collection you can use this update:
Update Mongo Playground