2

我有一个包含嵌套对象的 Word Schema,如下所示。如果我想在“elementary_one_reading”中搜索值为xxx的单词,我可以简单地做db.words.find({"apperance.elementary_one_reading": xxx})

但是,我想知道如何在猫鼬中做到这一点,假设我想为键传递一个变量。特别是,我希望有一种方法可以执行以下操作,但它不起作用。

function(key, value) {
    Word.find({"appearance." + key: value}, function(err, results) {
    }

}


{ "word" : "岁", "_id" : ObjectId("1"), "appearance" : { "elementary_one_reading" : [ 1 ] }}
{ "word" : "女", "_id" : ObjectId("2"), "appearance" : { "elementary_one_reading" : [ 1 ] }}
{ "word" : "我", "_id" : ObjectId("3"), "appearance" : { "elementary_two_reading" : [ 1, 8 ] }}
{ "word" : "你", "_id" : ObjectId("4"), "appearance" : { "elementary_two_reading" : [ 1, 2 ] }}
4

1 回答 1

0

I just realised that the easy way to solve this is via the use of queries. I could have easily done the following.

var field = 'appearance.elementary_one_reading'; Word.find().where(field, xxx).exec(callback);

于 2013-08-27T08:39:10.527 回答