Keystone-classic: How to put more than one item in many o many Relationship Field?

Created on 16 Apr 2016  ·  2Comments  ·  Source: keystonejs/keystone-classic

First of all, Keysone.js is great and I love it, but I can't figure out how to put more than one item in many o many Relationship Field with javascript (and what is easily done in adminUI).
This is my model:

var Event = new keystone.List('Event', {
    autokey: {path: 'event', from: 'title', unique: true},
    map: {name: 'title'},
    track: true,
    defaultSort: '-createdAt'
});

Event.add({
    title: {type: String, index: true, initial: true, required: true},
    latitude: {type: Types.Number, initial: true, required: true},
    longitude: {type: Types.Number, initial: true, required: true},
    eventImg: {type: Types.CloudinaryImages, publicID: 'event', folder: 'events', autoCleanup : true, collapse: true},
    description: {type: String, initial: true, default: "Event desc"},
    rHappiness : {type: Types.Number, initial: true, default: 0},
    rHealth: {type: Types.Number, initial: true, default: 0},
    rKnowledge: {type: Types.Number, initial: true, default: 0},
    location: {type: Types.Location, initial: true, required: false}, // dodatan info 

    doneTimes: {type: Types.Number, default: 0, noedit: true},

    hrkiLooksUnlocked: {type: Types.Relationship, ref: 'Hrkilook', many: true, initial: true}
});

And when I want to add one more 'Hrkilook' to hrkiLooksUnlocked field :

keystone.list('Event').findOneAndUpdate(
       {_id: event_id},
       {hrkiLooksUnlocked: newHrkilook_id},
       function(err){...}
);

it rewrites the old value (what i find perfectly logical - update).
Also if I want to update field by appending + ',' + newHrkilook_id, I get an error that new value is not an ObjectID. I searched official docs and mongoose docs, but haven't found anything.
Any help would be appreciated!

Most helpful comment

@ivanoreh have you tried something like:

keystone.list('Event').findOneAndUpdate(
       {_id: event_id},
       {$addToSet: {hrkiLooksUnlocked: newHrkilook_id}},
       function(err){...}
);

All 2 comments

@ivanoreh have you tried something like:

keystone.list('Event').findOneAndUpdate(
       {_id: event_id},
       {$addToSet: {hrkiLooksUnlocked: newHrkilook_id}},
       function(err){...}
);

@webteckie Thank you very much!
I tried with $push: , but $addToSet was just what I was looking for :)

Was this page helpful?
0 / 5 - 0 ratings