[英]How to convert this mongo query to pymongo code?
我寫了一些mongo查詢。 但我不能在pymongo中使用此查詢。
我嘗試使用“ $ lookup”,但是我的mongo版本是3.4,它不支持管道。 所以我寫了查詢。 但是我不知道將查詢結果存儲到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).
在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)
因此,在您的情況下,它將遵循以下原則:
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}}
]}
您也不需要python中的var
。 與python中一樣,您無需聲明變量類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.