简体   繁体   English

Pymongo在条件和按日期分组的多个集合上选择值

[英]Pymongo select value on multiple collections where condition, and group by date

Am new in mongodb and I work using pymongo 是mongodb的新手,我使用pymongo

Is there a way to select specific field in multiple collections where specific date matches query parameter, and group records by date? 有没有一种方法可以在多个集合中选择特定日期与查询参数匹配的特定字段,并按日期对记录进行分组?

Demonstration: 示范:

I have 2 collections A, B 我有2个收藏品A,B

A: A:

{
   "_id" : ObjectId("7d663451d1e7242c4b68ekjd"), 
   "date" : "Mon Dec 27 2010 18:51:00 GMT+0000 (UTC)", 
   "value" : 1, 
}
{
   "_id" : ObjectId("qd663451d1e7242c4b68e001"), 
   "date" : "Mon Dec 27 2010 18:52:00 GMT+0000 (UTC)", 
   "value" : 2, 
}
...
...

B: B:

{
   "_id" : ObjectId("td663451d1e7242c4b68eiu6"), 
   "date" : "Mon Dec 27 2010 18:51:00 GMT+0000 (UTC)", 
   "prediction_value" : 3, 
}
{
   "_id" : ObjectId("4d663451d1e7242c4b68e004"), 
   "date" : "Mon Dec 27 2010 18:52:00 GMT+0000 (UTC)", 
   "prediction_value" : 4, 
}
...
...

eg: 例如:

If I search for date == Mon Dec 27 2010 18:52:00 GMT+0000 (UTC) 如果我搜索日期== 2010年12月27日星期一18:52:00 GMT + 0000(UTC)

I need to get A.value and B.prediction_value and group records by this date 我需要在此日期之前获取A.value和B.prediction_value以及组记录

result like: 结果像:

"Mon Dec 27 2010 18:52:00 GMT+0000": [ 
 { 
   "A" : 2
 },
 {
   "B" : 4
 }
]

I would greatly appreciate some example as I am a novice. 我是个新手,我将不胜感激。 Thank's for your comprehension. 感谢您的理解。

You can do two easy queries separately: 您可以分别进行两个简单的查询:

date = 'Mon Dec 27 2010 18:52:00 GMT+0000'
A_doc = db.A.find_one({'date': date})
B_doc = db.B.find_one({'date': date})
A_value = A_doc['value']
B_prediction_value = B_doc['prediction_value']
# manipulate the data as you want

Mongo doesn't have full support of multi-collection queries, only $lookup for left outer join. Mongo没有完全支持多集合查询,只有$ lookup用于左外部联接。 But I think that doing two simple queries is more preferable than one difficult, in most cases. 但是我认为在大多数情况下,执行两个简单的查询比解决一个困难的查询更可取。 Check this links for more info: 检查此链接以获取更多信息:

Search on multiple collections in MongoDB 在MongoDB中搜索多个集合

MongoDB query multiple collections at once MongoDB一次查询多个集合

https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/ https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM