jeudi 24 août 2017

mongodb random sample strange behavior

I am using:

mongodb server 3.47
windows 10 64-bit
python 3.62 64-bit
pymongo 3.50

There are two record in "k" collection in "dict" database:

{"text": "xdcdcdcd", "sent": "false"}
{"text": "vvrvrrrv", "sent": "true"}

I want to choose a random record which has "sent" equals false:

from pymongo import MongoClient
client = MongoClient()
db = client.dict
k = db.k
item = list(k.aggregate([{"$sample": {"size": 1}}, {"$match": {"sent": False}}]))

The variable item should be "[{"_id":".....", "text": "xdcdcdcd", "sent": "false"}]", but I tried several times, sometimes it works fine, but sometimes it returns [].




Aucun commentaire:

Enregistrer un commentaire