Friday, 19 May 2017

fetching values from nested array in mongoose for autosuggestion

I want an autosuggestion feature in my tool for which ineed values from a particular block from all the document in mongo collection . the block is an array block which is like:

relations:[
        {
            "id" : "1", 
            "name" : "hasMembers", 
            "divId" : "name_div"
        }, 
        {
            "id" : "2", 
            "name" : "hasPublication", 
            "divId" : "name_div"
        }, 
        {
            "id" : "3", 
            "name" : "hasInstitution", 
            "divId" : "name_div"
        }, 
        {
            "id" : "4", 
            "name" : "alsoKnownAs", 
            "divId" : "name_div"
        }, 
        {
            "id" : "5", 
            "name" : "hasCoAuthors", 
            "divId" : "name_div"
        }, 
        {
            "id" : "6", 
            "name" : "isPublishedBy", 
            "divId" : "name_div"
        }, 
        {
            "id" : "7", 
            "name" : "isAuthoredBy", 
            "divId" : "name_div"
        }, 
        {
            "id" : "8", 
            "name" : "hasMemberPublication", 
            "divId" : "name_div"
        }, 
        {
            "divId" : "name_div", 
            "name" : "hasmemberPubs", 
            "id" : "R3"
        }, 
        {
            "divId" : "name_div", 
            "name" : "hasmemberPubs", 
            "id" : "R4"
        }, 
        {
            "divId" : "name_div", 
            "name" : "hasmemberPubssssssss", 
            "id" : "R5"
        }, 
        {
            "divId" : "name_div", 
            "name" : "hasmemberPubsssssssseeeeeerrrr", 
            "id" : "R9"
        }, 
        {
            "divId" : "name_div", 
            "name" : "hasunst", 
            "id" : "R10"
        }
    ]

Likewise each document in the collection has similar block My query for search term Eg.hasMembers goes like this in express js

 relconf.find({"relations.name":"hasMembers"},'relations.name',function(err, users) {
        res.send(users)
        })

NOTE: Here reconf is my mongoose schema : relconfig.js

var mongoose = require('mongoose');
var subSchema = mongoose.Schema({
    name:String,
    divId:String,
    id:String 
},{ _id : false });
var relconfig = new mongoose.Schema({
    name:{ type: String, 
        unique: true,},
    pId:String,
    db: {dbName:String,
        collection:Array},
    readOnly:Boolean,
    createDate:String,
    active:Boolean,
    relations:[
        subSchema
    ]
   },{collection:'configtest'});

mongoose.model('relconfig',relconfig);

module.exports = mongoose.model('relconfig');

Upon executing the API above i am getting response as array of arrays (from multiple documents ) where relations.name:hasMembers

[
  {
    "_id": "5909d148a6e483684133f329",
    "relations": [
      {
        "name": "hasMembers"
      },
      {
        "name": "hasPublication"
      },
      {
        "name": "hasInstitution"
      },
      {
        "name": "alsoKnownAs"
      },
      {
        "name": "hasCoAuthors"
      },
      {
        "name": "isPublishedBy"
      },
      {
        "name": "isAuthoredBy"
      },
      {
        "name": "hasMemberPublication"
      },
      {
        "name": "hasmemberPubs"
      },
      {
        "name": "hasmemberPubs"
      },
      {
        "name": "hasmemberPubssssssss"
      },
      {
        "name": "hasmemberPubsssssssseeeeeerrrr"
      },
      {
        "name": "hasunst"
      }
    ]
  },
  {
    "_id": "59115e5da6e483756720da88",
    "relations": [
      {
        "name": "hasMembers"
      },
      {
        "name": "hasInstitution"
      },
      {
        "name": "alsoKnownAs"
      },
      {
        "name": "hasCoAuthors"
      },
      {
        "name": "isPublishedBy"
      },
      {
        "name": "isAuthoredBy"
      },
      {
        "name": "hasMemberPublication"
      }
    ]
  }
]

As you can see i am getting unwanted results apart from "hasMembers" How can get only single result like

{name:hasMembers} i am only intrested in "hasMembers" value which i will stor in another arry. MY problem is iteration within the array to get the single json PLease help



via Nitika Jain

No comments:

Post a Comment