lal_bosdi
lal_bosdi

Reputation: 173

Get Object Id in mongo shell script for a collection object just created

I am creating a shell script for mongodb, which seeds test data for dev env. Initially I had created 2 users in a collection called users with db.save command.

Now I want to create 2 objects in a collection called containers which stores the object id of the 2 users created as owners. How can i do this.

WS1 = db.containers.save(
  {
    name: "Test WS 1",
    created: Date.now(),
    updated: Date.now(),
    owners: [user_1, user_2]
  }
);

Instead of user_1 and user_2 i want to store the ids of the user objects created before in the script.

Thanks in advance.

Upvotes: 1

Views: 2112

Answers (1)

Ori Dar
Ori Dar

Reputation: 19000

var a = {name : "John"}
db.users.save(a);

var b = {name : "Paul"}
db.users.save(b);

var users = []
users.push(a._id, b._id)

db.containers.save(
{
     name: "Test WS 1",
     created: new Date(),
     updated: new Date(),
     owners: users
});                

Upvotes: 2

Related Questions