给定一个集合:
{id: 1, name:"abc"},
{id: 2, name:null}
要测试我使用的属性:
db.collecton.find({name:"abc"}, {name:1}); // which returns the first document.
我还想添加两个外部值的测试,如下所示:
value1 = 100;
value2 = 100; // value2 might be different than value1
db.collecton.find({name:"abc", $eq:[ value1, value2 ]}, {name:1});
这导致什么也没找到。我不想添加 value1 或 value2 作为属性,我只想确保除了其他条件之外它们彼此匹配。另外,我意识到我只能在两个外部值匹配的情况下运行查询,但是如何将这些外部变量添加到条件中?
最佳答案
db.collection.find({ name : 'abc', name : { $type : 1 + (value1 === value2) }})
关于javascript - 如何测试 mongodb criteria 对象中的两个外部值是否相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29567495/