[英]How to convert this mongo query to pymongo code?
I have wrote some mongo query. 我写了一些mongo查询。 but I can't use this query in pymongo.
但我不能在pymongo中使用此查询。
I tried to use "$lookup" but my mongo version is 3.4, and it doesn't support pipeline. 我尝试使用“ $ lookup”,但是我的mongo版本是3.4,它不支持管道。 So I wrote queries.
所以我写了查询。 But I have no idea to store query results to mongodb 'var' in python.
但是我不知道将查询结果存储到python中的mongodb'var'中。
var user_ids = db.register.find({
"$and": [
{"_id": {"$gte": ObjectId("5d0a4df00000000000000000")}}
,{"_id": {"$lt": ObjectId("5d0b9f700000000000000000")}}
]}).map(function(register){
return register.user_id;
});
db.login.find({
"$and": [
{"_id": {"$gte": ObjectId("5d0b9f700000000000000000")}}
,{"_id": {"$lt": ObjectId("5d0cf0f00000000000000000")}}
,{"user_id": {"$in": user_ids}}
]});
I wanna convert those queries to python code(pymongo).
From W3Schools you can create queries like so 在W3Schools中,您可以像这样创建查询
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
for x in mycol.find({},{ "_id": 0, "name": 1, "address": 1 }):
print(x)
so in your case it would be along the lines of: 因此,在您的情况下,它将遵循以下原则:
from bson.objectid import ObjectId
user_ids = db.register.find({},{
"$and": [
{"_id": {"$gte": ObjectId("5d0a4df00000000000000000")}}
,{"_id": {"$lt": ObjectId("5d0b9f700000000000000000")}}
]}).map(function(register){
return register.user_id;
});
db.login.find({}, $and": [
{"_id": {"$gte": ObjectId("5d0b9f700000000000000000")}}
,{"_id": {"$lt": ObjectId("5d0cf0f00000000000000000")}}
,{"user_id": {"$in": user_ids}}
]}
You also do not need the var
in python. 您也不需要python中的
var
。 As in python you do not need to declare the variable type. 与python中一样,您无需声明变量类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.