A
A
Abc Edc2015-04-27 09:56:00
MongoDB
Abc Edc, 2015-04-27 09:56:00

How to organize storage and correctly update ObjectId in an array?

Scheme

var schema = new Schema({
    mail:{
        type: String,
        unique: true,
        required: true
    },
    hashedPassword: {
        type: String,
        required: true
    },
    salt:{
        type: String,
        required: true
    },
    name:{
        type: String,
        required: true
    },
    surname:{
        type: String,
        required: true
    },
    avatar:{
        type: String
    },
    date:{
        type: Date,
        default:  Date.now
    },
    friends: [
        Schema.Types.ObjectId
    ]
});

it is assumed that friends
[
  {id}
  {id}

]

calling
var addingId = new ObjectID(req.params.id1);
   var added = new ObjectID(req.params.id2);
 User.insert({_id:addingId},{friends: [added]},function(){
        console.log('НАКОНЕЦ ТО ЗАРАБОТАЛО');
    })

grab

TypeError: Object function model(doc, fields, skipId) {
if (!(this instanceof model))
return new model(doc, fields, skipId);
Model.call(this, doc, fields, skipId);
} has no method 'insert'

How to fix it right

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Abc Edc, 2015-04-27
@gleber1

Response:User.update({_id:addingId}, { $addToSet: { friends: added } },function(){
console.log('ddd');
})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question