Defining a map with ObjectId key and array of strings as value in mongoose schema
Asked Answered
P

1

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?

Pelpel answered 16/8, 2015 at 18:39 Comment(0)
C
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 })
Chirk answered 17/8, 2015 at 9:8 Comment(6)
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"]}Pelpel
How will you handle multiple myIds in your existing schema ?Pelpel
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.Chirk
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 recordPelpel
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 onlyPelpel
In that case, I'd retrieve the relevant fields and update it. Might be relevant: #23471158Chirk

© 2022 - 2024 — McMap. All rights reserved.