MongoDB - How to insert an ObjectID via variable?

2

I'm learning MongoDB and I had a problem, I'm trying to insert an ObjectID into an array via variable, but I'm not getting it. I'm doing the following:

> var aluno = db.alunos.find({"nome" : "leandro"});
> aluno;
{ "_id" : ObjectId("5ada02cc6c0218c2b64e0214"), "nome" : "leandro", "data_nasc" : ISODate("1996-07-19T03:00:00Z") }

Now when I try to insert only the "_id" field, the following occurs:

> db.professores.update({"nome" : "leandro"}, {$push : {"alunos" : aluno["_id"]}}) 
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.professores.find({ })
{ "_id" : ObjectId("5ad55f59f034bcd625334947"), "nome" : "leandro", "curso" : "guitarra", "alunos" : [ undefined ] }

The value entered is "undefined". What am I doing wrong?

    
asked by anonymous 20.04.2018 / 17:36

0 answers