2014-04-20 49 views
0

我有證件的MongoDB的這樣一個集合:

{ 
    "recipe_id": "123", 
    "recipe_name": "Pizza", 
    "recipe_author": "Jane Doe" 
{ 

然後,在JavaScript中,我有一個對象像這樣的數組:

[{ 
    "author": "John Doe" 
} 
{ 
    "author": "Jane Doe" 
}] 

我想查找至少一個文檔,其中MongoDB中的recipe_author字段與我的JavaScript數組對象中的作者字段相匹配,因此我可以獲取author_id。

如果我的研究者們在一個數組,我可以檢查他們一下子像這樣:

["Jane Doe", "Billy Bob", "Another Author"] 

collection.findOne({ 
    'author_name': { $in: authors } 
} 

但是,他們是對象的數組。是否有可能根據這些方面做些事情?

collection.findOne({ 'author_name': { $in: <each author field in my object> } } 

回答

0

我認爲在構建迭代裏面$作者可能是不可能的,但可以形成之前查詢 所需的陣列。

var author = [{ 
     "author": "John Doe" 
    } 
    { 
     "author": "Jane Doe" 
    }]; 

    var authorObj = author.map(function(eachObj){ 
        return eachObj.author; 
    }); 

    collection.findOne({ 
     'author_name': { $in: authorObj } 
    }