Searching for items in a JSON array Using Node (preferably without iteration)

后端 未结 6 900
孤城傲影
孤城傲影 2020-12-16 03:06

Currently I get back a JSON response like this...

{items:[
  {itemId:1,isRight:0},
  {itemId:2,isRight:1},
  {itemId:3,isRight:0}
]}

I want

6条回答
  •  既然无缘
    2020-12-16 03:22

    Actually I found an even easier way if you are using mongoDB to persist you documents...

    findDocumentsByJSON = function(json, db,docType,callback) {
      this.getCollection(db,docType,function(error, collection) {
        if( error ) callback(error)
        else {
          collection.find(json).toArray(function(error, results) {
            if( error ) callback(error)
            else
              callback(null, results)
          });
        }
      });
    }
    

    You can then pass {isRight:1} to the method and return an array ONLY of the objects, allowing me to push the heavy lifting off to the capable mongo.

提交回复
热议问题