0

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
       } 
    ] 
}
2
  • I didn't understand what you want to achieve, can you explain again? Commented Mar 6, 2017 at 7:55
  • I want to find the same value in the db as the following itemTag array. { "ItemTag": [{ "Main": 3, "Sub": 7, "Color": 1 }, { "Main": 3, "Sub": 1, "Color": 11 }] } Commented Mar 6, 2017 at 8:01

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

thanks.. but i got this message : { "message": "Cast to number failed for value \"[ { main: 3, sub: 7, color: 1 }, { main: 4, sub: 4, color: 1 } ]\" at path \"main\" for model \"Item\"", "name": "CastError", "stringValue": "\"[ { main: 3, sub: 7, color: 1 }, { main: 4, sub: 4, color: 1 } ]\"", "kind": "number",
Please post what query you are execute ,, and make sure that all documents fields( "main" , "sub" and "color" are number field)
0

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}})

Comments

0

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.......

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.