$nin不工作mongodb Atlas触发器NodeJS



我的Atlas触发器函数中有以下代码:

const existings = await test_collection.find().toArray();
var ids = [];

for (let i = 0; i < existings.length; i++) {
ids.push(existings[i]._id);
}
console.log("Existing ids : ", ids);
const value = await user_collection.find({"_id": {$nin: ids}}).sort({quantity: -1}).limit(1);
console.log("Value is : ", JSON.stringify(value));

日志显示:

Logs:
[
"Existing ids :  vfLGh8QVOsbmz1,F7Mqe1YwH5fsV83",
"Value is :  {}",
]

实际工作的python等价物:

def test(self):
test = self.db.test.find()
ids = []
for t in test:
ids.append(t["_id"])
print(f"Existing ids : {ids}")
value = self.db.users.find({"_id": {"$nin": ids}}).sort("quantity", pymongo.DESCENDING).limit(1)
print(f"Value is : {value[0]}")

和python日志:

Existing ids : ['vfLGh8QVOsbmz1', 'F7Mqe1YwH5fsV83']
Value is : Value is : {'_id': '6GzgNoZFR2H7hfdzI3', 'username': 'test3'}

我已经尝试过在没有sort运算符的情况下确保问题会来自$nin,我有同样的空输出,没有sort

您需要在limit(1(之后添加toArray()方法来转换值,而不需要获得游标。

这看起来像:

const value = await user_collection.find({"_id": {$nin: ids}}).sort({quantity: -1}).limit(1).toArray();

并且您的value常量应该具有结果。

最新更新