greatsk
greatsk

Reputation: 23

Mongoose Query: how to find object array

If you send json data in the following format, the server tries to go through the item document of mongodb and write the code that returns the data with the same userId and exactly the same json data received by itemTag.

I have this schema:

var subSchema = mongoose.Schema({
  main:Number,
  sub:Number,
  color:Number
},{ _id : false });

var ItemSchema = new Schema({
    userId:String,
    date:String,
    itemTag: [subSchema]
}, { versionKey: false });

db data :

{ 
   "_id" : ObjectId("58ba81eea341c37ed7268703"),
   "date" : "20170304_175923", 
   "userId" : "aaa", 
   "itemTag" : [ 
       { 
           "main" : 3, 
           "sub" : 7,  
           "color" : 1
       }, { 
           "main" : 3, 
           "sub" : 1,  
           "color" : 11
       }, { 
           "main" : 4, 
           "sub" : 4,  
           "color" : 1
       }, {
           "main" : 5, 
           "sub" : 2,  
           "color" : 1
       } 
    ] 
},
{ 
   "_id" : ObjectId("58ba81eea341c37ed7268723"),
   "date" : "20170305_125923", 
   "userId" : "aaa", 
   "itemTag" : [ 
       { 
           "main" : 3, 
           "sub" : 7,  
           "color" : 1

       }, { 
           "main" : 2, 
           "sub" : 2,
           "color" : 2
       } 
    ] 
}

**and client send json data : ** server get req.body.userId req.body.itemTag

{ 
    "userId":"aaa",
    "itemTag":[{
        "main":3,
        "sub":7,
        "color":1
    },{
        "main":4,
        "sub":4,
        "color":1
    }]
}

and i want get :

{ 
   "_id" : ObjectId("58ba81eea341c37ed7268703"),
   "date" : "20170304_175923", 
   "userId" : "aaa", 
   "itemTag" : [ 
       { 
           "main" : 3, 
           "sub" : 7,  
           "color" : 1
       }, { 
           "main" : 3, 
           "sub" : 1,  
           "color" : 11
       }, { 
           "main" : 4, 
           "sub" : 4,  
           "color" : 1
       }, {
           "main" : 5, 
           "sub" : 2,  
           "color" : 1
       } 
    ] 
}

Upvotes: 0

Views: 4479

Answers (3)

s7vr
s7vr

Reputation: 75934

You can try below find query. The query uses $all with $elemMatch to return rows when there is entry for each of the values in the array.

var rItemTag = req.body.itemTag
var qItemTag = rItemTag.map(value => ({"elemMatch": value}));
db.collection.find({itemTag: {$all: qItemTag}})

Upvotes: 0

Parshuram Kalvikatte
Parshuram Kalvikatte

Reputation: 1646

Try This

   db.collection.find(
        {"userId" : req.body.userId,"itemTag.main" : req.body.itemTag},
        {itemTag : 1,userId:1})
        .sort(sortQuery)
        .skip(skipPage)
        .limit(16)

OR

 db.collection.find(
        {"userId" : req.body.userId,"itemTag":{ "$elemMatch" : {main : req.body.itemTag}}},
        {itemTag : 1,userId:1})
        .sort(sortQuery)
        .skip(skipPage)
        .limit(16)



//Note : In collection you need to write your collectionName

3 ) - If you want a specific elements from array

db.collection.find(
  {"userId" : req.body.userId,"itemTag":{ "$elemMatch" : {main : req.body.itemTag}}},
     {"itemTag.$.main"  : 1,userId:1})
     .sort(sortQuery)
    .skip(skipPage)
    .limit(16)

Upvotes: 1

Vishal
Vishal

Reputation: 6368

I am not sure, but you should use:

itemTag.$.main syntax

For getting first item in the array you can use:

itemTag.$0.main

For second item in the array:

itemTag.$1.main

and so on.......

Upvotes: 0

Related Questions