Mongoose:在条件为数组的情况下发出请求

时间:2018-02-22 19:22:12

标签: node.js mongodb mongoose mongodb-query

我正在尝试从Node.js服务器向Mongoose发出请求。在集合中,文档具有如下属性:

industryIdentifiers:[{
  type: { type: String, required: false },
  identifier: { type: String, required: false }
}],

我的目的是查询具有相同属性的文档。我试过这样:

`Book.find({industryIdentifiers: book.volumeInfo.industryIdentifiers},(err, findedBook) => { //some process here })`

其中book.volumeInfo.industryIdentifiers的格式如下:

[{"type":"ISBN_13","identifier":"9781781103524"},{"type":"ISBN_10","identifier":"1781103526"}]

这不起作用,但我不知道如何用Mongoose执行这种请求。

我真的很感激这方面的帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

你可以这样做

   var book = {};
   book['data'] = [];

   for(i in book.volumeInfo.industryIdentifiers){

        Book.find({industryIdentifiers: $elemMatch : 
                  {type   :  book.volumeInfo.industryIdentifiers[i].type ,
                  identifier : book.volumeInfo.industryIdentifiers[i].identifier}

                   },(err, findedBook) => {

                          book['data'].push({
                              result : findedBook
                          });
            });
   }

     //Now your find ouptut is in book.data
     You can perform your action here  with book.data
        //some process here