NeDB 如何在查找查询中使用变量?

NeDB How can i use variable in find query?

如何在查找查询中使用变量,如下所示:

db 有

这样的文档
{choices:{'04-09-2017':'a'},b:'b'},
{choices:{'04-10-2017':'a'},c:'c'}

我的查询是

 var x = "choices.04-09-2017"
 db.find({
        x: {
            $exists: true
        }
    }, function(err, docs) {

    });

我如何定义 x 是一个变量而不是 属性? 谢谢!

如果您定义一个代表您的搜索的对象会更容易:

var toFind = {};
var firstTerm = "choices";
var secondTerm = "04-09-2017";
toFind[firstTerm]={};
toFind[firstTerm][secondTerm] = {$exists = true};

db.find(toFind, function(err, docs) {
  // put here your callback
});

这应该有效。

在你的例子中

{
  x: {
    $exists: true
  }
}

被视为搜索对象的键,不被评估。