How to execute a query through DBRef in MongoDB / pymongo?

Is it possible to query through DBRef with a single search specification?

user collection

{ 'age': 30 } 

postage

 { 'user': DBRef('user', ...) } 

Is it possible to request for all users who have 30 users at one stage of the search? If not, would it be wise to create a javascript function to handle a multi-step operation, or cause blocking problems?

+4
source share
3 answers

this is impossible to do. I would recommend:

a) changing your data model so that all the data is in one document (perhaps this is not possible depending on your case).

b) a request for users who are first 30, and then a second request to receive messages in which the user is $ in this list. I would do this client side, and not use server-side JS or something like that.

+6
source

I am using the Python driver, so please forgive my syntax not so-mongodb:

 users = list(db.Users.find({'Age':30})) posts = list(db.Posts.find({'User':{'$in':users}})) 
+1
source

Install the python bson package. and try as an example.

 import pymongo from pymongo import MongoClient from bson.dbref import DBRef client = MongoClient('ip', 27017) client.the_database.authenticate('user', 'password', source='db_name') db = client['db_name'] user = db['user'] user_id = user.find_one({'email': ' xxxxx@gmail.com '}).get('_id') client_user_relation = db['client_user_relation'] print(client_user_relation.find_one()) print(user_id) print(DBRef(collection = "user", id = user_id)) print(client_user_relation.find_one({'user': DBRef(collection = "user", id = user_id)})) 
0
source

Source: https://habr.com/ru/post/1310265/


All Articles