2

I'm facing a problem while creating Mongoose schema for my DB. I want to create a map with a objectId as key and an array of string values as its value. The closest that I can get is:

var schema = new Schema({
   map: [{myId: {type:mongoose.Schema.Types.ObjectId, ref: 'MyOtherCollection'}, values: [String]}]
});

But somehow this is not working for me. When I perform an update with {upsert: true}, it is not correctly populating the key: value in the map. In fact, I'm not even sure if I have declared the schema correctly.

Can anyone tell me if the schema is correct ? Also, How can I perform an update with {upsert: true} for this schema?

Also, if above is not correct and can;t be achieved then how can I model my requirement by some other way. My use case is I want to keep a list of values for a given objectId. I don't want any duplicates entries with same key, that's why picked map.

Please suggest if the approach is correct or should this be modelled some other way?

Update:

Based on the answer by @phillee and this, I'm just wondering can we modify the schema mentioned in the accepted answer of the mentioned thread like this:

{
    "_id" : ObjectId("4f9519d6684c8b1c9e72e367"),
    ... // other fields
    "myId" : {
        "4f9519d6684c8b1c9e73e367" : ["a","b","c"],
        "4f9519d6684c8b1c9e73e369" : ["a","b"]
    }
}

Schema will be something like:

var schema = new Schema({
   myId: {String: [String]}
});

If yes, how can I change my { upsert:true } condition accordingly ? Also, complexity wise will it be more simpler/complex compared to the original schema mentioned in the thread?

Community
  • 1
  • 1
dark_shadow
  • 3,503
  • 11
  • 56
  • 81

1 Answers1

0

I'd suggest changing the schema so you have one entry per myId,

var schema = new Schema({
  myId : {type:mongoose.Schema.Types.ObjectId, ref: 'MyOtherCollection'},
  values : [String]
})

If you want to update/upsert values,

Model.update({ myId : myId }, { $set : { values : newValues } }, { upsert : true })
phillee
  • 2,227
  • 2
  • 19
  • 28
  • I think I haven't explained the question properly. You can have multiple myId's in a single document with no duplicate for a same myId. So, if you have {1:["a"] , 2:["b"]} and if there is some new entry for {1: ["c"]}, then it should become {1:["c"] , 2:["b"]} – dark_shadow Aug 17 '15 at 09:21
  • How will you handle multiple myIds in your existing schema ? – dark_shadow Aug 17 '15 at 09:23
  • Right. With this modified schema, when you do Mode.update({ myId : myId }) and upsert, this will modify any existing record with myId == myId and create a new record if the myId doesn't exist yet. – phillee Aug 17 '15 at 09:28
  • No, I don't want another record. I think we should have some way of keeping this information in existing record. If new myId, then insert new pair otherwise update existing pair within a single record – dark_shadow Aug 17 '15 at 10:01
  • Also, fyi this is just a part of a bigger schema in my db. In order to keep things simple, I have given the exact problem. I can't create a separate collection for this. I want to keep this info in a single record only – dark_shadow Aug 17 '15 at 10:02
  • In that case, I'd retrieve the relevant fields and update it. Might be relevant: http://stackoverflow.com/questions/23470658/mongodb-upsert-sub-document – phillee Aug 17 '15 at 10:10