0

Below is the simple JSON format for a stored document in my Chats collection using Meteor 1.6 and MongoDB 3.4

{ 
    "_id" : "PN27eK2DWoRXmFrzB", 
    "members" : [
        "BtKnXevDi3hm8CmMP", 
        "whLGMxdPwFkR6sNoy"
    ], 
    "details" : [
        {
            "message" : "cool", 
            "sender" : "BtKnXevDi3hm8CmMP", 
            "read" : false, 
        }, 
        {
            "message" : "you there buddy?", 
            "sender" : "whLGMxdPwFkR6sNoy", 
            "read" : false, 
        }
    ]
}

Simple Schema looks like below;

ChatsSchema = new SimpleSchema({
    'members': { type: Array, optional: true, minCount: 2, maxCount: 2 },
    'members.$': String,
    'details': { type: Array, optional: true },
    'details.$': Object,
    'details.$.sender': { 
        type: String, 
        optional: true,
        autoValue: function(){
            return Meteor.userId();
        }
    },
    'details.$.message': { type: String, min: 1, max: 200 },
    'details.$.read': { type: Boolean, defaultValue: false },
    'details.$.createdAt': {
        type: Date,
        autoValue: function(){ return new Date(); }
    },
},{ tracker: Tracker });

I have below query built so far to update nested data details.$.read.

let members = [ "BtKnXevDi3hm8CmMP", "whLGMxdPwFkR6sNoy"];
let otherUserId = 'BtKnXevDi3hm8CmMP'
Chats.update({
        members: {$in: members},
        'details' : { "$elemMatch": { "sender": otherUserId } }
    }, 
    { 
        $set: { 
            'details.$.read': true
        } 
    });

Expected output is such that details with sender as "BtKnXevDi3hm8CmMP" must have read field set to true, as below;

"details" : [
        {
            "message" : "cool", 
            "sender" : "BtKnXevDi3hm8CmMP", 
            "read" : true, 
        }, 
        {
            "message" : "you there buddy?", 
            "sender" : "whLGMxdPwFkR6sNoy", 
            "read" : false, 
        }, 
    ]

Need help to rectify my query and understand where I am going wrong?

Ankur Soni
  • 5,725
  • 5
  • 50
  • 81
  • Where are you running the statement from? Is this on the server or the browser? AFAIK minimongo updates require the `_id` of the document from the browser, but the statement should be fine from the server side. Are you certain multiple documents don't match the condition? Because that could be a cause as you don't have `{ multi: true }` ( in case of server execution of course ) – Neil Lunn May 09 '18 at 08:28
  • I am running this at the Server end, using Mongo 3.2, also tried `{multi: true}`, there will be no multiple documents with the same number of members. I did console and I do get the document count as `1` but the document is not updated. I also tried on Studio 3T and ran the raw query, still, it does not work. on Studio 3T console it shows `WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 }) ` – Ankur Soni May 09 '18 at 08:32
  • Odd indeed. Are you able to spin up a test case "without" using `SimpleSchema`? It just seems a bit odd to me that you're the second person in a couple of days with a similar update "oddity" where `SimpleSchema` is involved. It would be good to rule that out if nothing else, since the statement "should work". – Neil Lunn May 09 '18 at 08:34
  • @Neil: I am shocked to see that the query that I have specified in the question works but only updates the first element of the array only. Even Mongo DB doing the same, only updating first object of array `details`, here is the proof `WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })` – Ankur Soni May 09 '18 at 09:16
  • Why are you shocked? That's exactly what is supposed to happen. So it is actually updating or not? Or is your problem that you are expecting more array members to update? Is it just me or are you now asking something different from what you originally asked? – Neil Lunn May 09 '18 at 09:19
  • Definitely Array can have multiple `details` with `sender` as `xyz`, I want to update all, I dug further into mongodb, version 3.6 gives facility to update multiple objects in array using `arrayFilters` and I am using MongoDB 3.2, it is possible to update multiple objects in array in 3.2 as well? – Ankur Soni May 09 '18 at 09:22

0 Answers0