Python 在此类型的查询中查找或聚合
我的收藏由嵌套文档组成。 我的收藏是这样的:Python 在此类型的查询中查找或聚合,python,mongodb,pymongo,Python,Mongodb,Pymongo,我的收藏由嵌套文档组成。 我的收藏是这样的: db.mongodbbucketnocpu2index.find().pretty().limit(1) { "_id" : ObjectId("607f185f2a477a621641cded"), "nsamples" : 12, "samples" : [ { "id1" : 375
db.mongodbbucketnocpu2index.find().pretty().limit(1)
{
"_id" : ObjectId("607f185f2a477a621641cded"),
"nsamples" : 12,
"samples" : [
{
"id1" : 3758,
"id6" : 2,
"id7" : -79.09,
"id8" : 35.97,
"id9" : 5.5,
"id10" : 0,
"id11" : -99999,
"id12" : 0,
"id13" : -9999,
"c14" : "U",
"id15" : 0,
"id16" : 99,
"id17" : 0,
"id18" : -99,
"id19" : -9999,
"id20" : 33,
"id21" : 0,
"id22" : -99,
"id23" : 0,
"timestamp1" : ISODate("2010-01-01T00:00:00Z"),
"timestamp2" : ISODate("2009-12-31T19:05:00Z")
},
{
"id1" : 3758,
"id6" : 2,
"id7" : -79.09,
"id8" : 35.97,
"id9" : 5.5,
"id10" : 0,
"id11" : -99999,
"id12" : 0,
"id13" : -9999,
"c14" : "U",
"id15" : 0,
"id16" : 99,
"id17" : 0,
"id18" : -99,
"id19" : -9999,
"id20" : 33,
"id21" : 0,
"id22" : -99,
"id23" : 0,
"timestamp1" : ISODate("2010-01-01T00:05:00Z"),
"timestamp2" : ISODate("2009-12-31T19:05:00Z")
},
.
.
.
.
}
mydb1.mongodbbucketright.find(
{"samples.timestamp1": {"$gte": datetime.strptime("2010-01-01 00:05:00", "%Y-%m-%d %H:%M:%S"),
"$lte": datetime.strptime("2015-01-01 00:05:00", "%Y-%m-%d %H:%M:%S")},
"samples.id13":{"$gt":5}},
{"samples.$": 1 })
mydb1.mongodbbucketright.aggregate([
{
"$match": {
"samples.timestamp1": {"$gte": datetime.strptime("2010-01-01 00:05:00", "%Y-%m-%d %H:%M:%S"),
"$lte" :datetime.strptime("2015-01-01 00:05:00", "%Y-%m-%d %H:%M:%S")},
"samples.id13": {"$gt": 5}
}
},
{ "$unwind": "$samples" },
{
"$match": {
"samples.timestamp1": {"$gte": datetime.strptime("2010-01-01 00:05:00", "%Y-%m-%d %H:%M:%S"),
"$lte": datetime.strptime("2015-01-01 00:05:00", "%Y-%m-%d %H:%M:%S")},
"samples.id13": {"$gt": 5}
}
},
])
我有一个如下的查询:
db.mongodbbucketnocpu2index.find().pretty().limit(1)
{
"_id" : ObjectId("607f185f2a477a621641cded"),
"nsamples" : 12,
"samples" : [
{
"id1" : 3758,
"id6" : 2,
"id7" : -79.09,
"id8" : 35.97,
"id9" : 5.5,
"id10" : 0,
"id11" : -99999,
"id12" : 0,
"id13" : -9999,
"c14" : "U",
"id15" : 0,
"id16" : 99,
"id17" : 0,
"id18" : -99,
"id19" : -9999,
"id20" : 33,
"id21" : 0,
"id22" : -99,
"id23" : 0,
"timestamp1" : ISODate("2010-01-01T00:00:00Z"),
"timestamp2" : ISODate("2009-12-31T19:05:00Z")
},
{
"id1" : 3758,
"id6" : 2,
"id7" : -79.09,
"id8" : 35.97,
"id9" : 5.5,
"id10" : 0,
"id11" : -99999,
"id12" : 0,
"id13" : -9999,
"c14" : "U",
"id15" : 0,
"id16" : 99,
"id17" : 0,
"id18" : -99,
"id19" : -9999,
"id20" : 33,
"id21" : 0,
"id22" : -99,
"id23" : 0,
"timestamp1" : ISODate("2010-01-01T00:05:00Z"),
"timestamp2" : ISODate("2009-12-31T19:05:00Z")
},
.
.
.
.
}
mydb1.mongodbbucketright.find(
{"samples.timestamp1": {"$gte": datetime.strptime("2010-01-01 00:05:00", "%Y-%m-%d %H:%M:%S"),
"$lte": datetime.strptime("2015-01-01 00:05:00", "%Y-%m-%d %H:%M:%S")},
"samples.id13":{"$gt":5}},
{"samples.$": 1 })
mydb1.mongodbbucketright.aggregate([
{
"$match": {
"samples.timestamp1": {"$gte": datetime.strptime("2010-01-01 00:05:00", "%Y-%m-%d %H:%M:%S"),
"$lte" :datetime.strptime("2015-01-01 00:05:00", "%Y-%m-%d %H:%M:%S")},
"samples.id13": {"$gt": 5}
}
},
{ "$unwind": "$samples" },
{
"$match": {
"samples.timestamp1": {"$gte": datetime.strptime("2010-01-01 00:05:00", "%Y-%m-%d %H:%M:%S"),
"$lte": datetime.strptime("2015-01-01 00:05:00", "%Y-%m-%d %H:%M:%S")},
"samples.id13": {"$gt": 5}
}
},
])
位置投影在这里有用吗?
有时,我得到的结果似乎比预期的要少
所以我更改了查询,现在它看起来像这样:
db.mongodbbucketnocpu2index.find().pretty().limit(1)
{
"_id" : ObjectId("607f185f2a477a621641cded"),
"nsamples" : 12,
"samples" : [
{
"id1" : 3758,
"id6" : 2,
"id7" : -79.09,
"id8" : 35.97,
"id9" : 5.5,
"id10" : 0,
"id11" : -99999,
"id12" : 0,
"id13" : -9999,
"c14" : "U",
"id15" : 0,
"id16" : 99,
"id17" : 0,
"id18" : -99,
"id19" : -9999,
"id20" : 33,
"id21" : 0,
"id22" : -99,
"id23" : 0,
"timestamp1" : ISODate("2010-01-01T00:00:00Z"),
"timestamp2" : ISODate("2009-12-31T19:05:00Z")
},
{
"id1" : 3758,
"id6" : 2,
"id7" : -79.09,
"id8" : 35.97,
"id9" : 5.5,
"id10" : 0,
"id11" : -99999,
"id12" : 0,
"id13" : -9999,
"c14" : "U",
"id15" : 0,
"id16" : 99,
"id17" : 0,
"id18" : -99,
"id19" : -9999,
"id20" : 33,
"id21" : 0,
"id22" : -99,
"id23" : 0,
"timestamp1" : ISODate("2010-01-01T00:05:00Z"),
"timestamp2" : ISODate("2009-12-31T19:05:00Z")
},
.
.
.
.
}
mydb1.mongodbbucketright.find(
{"samples.timestamp1": {"$gte": datetime.strptime("2010-01-01 00:05:00", "%Y-%m-%d %H:%M:%S"),
"$lte": datetime.strptime("2015-01-01 00:05:00", "%Y-%m-%d %H:%M:%S")},
"samples.id13":{"$gt":5}},
{"samples.$": 1 })
mydb1.mongodbbucketright.aggregate([
{
"$match": {
"samples.timestamp1": {"$gte": datetime.strptime("2010-01-01 00:05:00", "%Y-%m-%d %H:%M:%S"),
"$lte" :datetime.strptime("2015-01-01 00:05:00", "%Y-%m-%d %H:%M:%S")},
"samples.id13": {"$gt": 5}
}
},
{ "$unwind": "$samples" },
{
"$match": {
"samples.timestamp1": {"$gte": datetime.strptime("2010-01-01 00:05:00", "%Y-%m-%d %H:%M:%S"),
"$lte": datetime.strptime("2015-01-01 00:05:00", "%Y-%m-%d %H:%M:%S")},
"samples.id13": {"$gt": 5}
}
},
])
我希望查询得到优化
你对此有什么看法?
我选对了吗?
将mongodb与pymongo一起使用
提前谢谢