Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Why is an _id with ObjectID added to when using MongoDB's $push to add new object to an array?

Why is an _id with ObjectID added to when using MongoDB's $push to add new object to an array?

Problem

I am using Node.js and Mongoose. player and tournament variables are Mongoose objects, fetched just before.

I want to add a new tournamentSession object (NOT a Mongoose object) into the tournamentSessions field of the player object. I am using the findOneAndUpdate to be able to make sure that I dont add the same tournement twice (using the "$ne")

Player.findOneAndUpdate({
            '_id': player._id,
            'tournamentSessions.tournament': {
                '$ne': tournament._id
            }
        }, {
            '$push': {
                'tournamentSessions': {
                    'tournament': tournament._id,
                    'level': 1,
                    'status': 'LIMBO',
                    'score': 0,
                }
            }
        }, function(err, updatedPlayer) {
            // ...
        });

Everything works fine, except that the "_id" field containing an ObjectID is added to the newly added session inside the tournamentSessions array, and I cant understand why this is happening.

If I manually add the "_id" field and with the value "BLABLABLA", the "_id" field is never stored (as it shouldnt, coz its not declared in the schema)

Ye, And here is the schemas:

var Player = mongoose.model('Player', Schema({
    createdAt: { type: Date, default: Date.now },
    lastActiveAt: Date,
    clientVersion: String,
    tournamentSessions: [{
        tournament: { type: Schema.Types.ObjectId, ref: 'Tournament' },
        level: Number,
        status: String,
        score: Number
    }],
    friends: Array
}));

var Tournament = mongoose.model('Tournament', Schema({
    createdAt: { type: Date, default: Date.now },
    open: Boolean,
    number: Number,
    level: Number,
    reactionLimit: Number,
    deadlineAt: Date,
    stats: {
        total: Number,
        limbo: Number,
        blessed: Number,
        damned: Number
    }
}));
Problem courtesy of: bobmoff

Solution

You can disable the _id field by explicitly defining the tournamentSessions array with its own schema so that you can set its _id option to false:

var Player = mongoose.model('Player', Schema({
    createdAt: { type: Date, default: Date.now },
    lastActiveAt: Date,
    clientVersion: String,
    tournamentSessions: [new Schema({
        tournament: { type: Schema.Types.ObjectId, ref: 'Tournament' },
        level: Number,
        status: String,
        score: Number
    }, { _id: false })],
    friends: Array
}));
Solution courtesy of: JohnnyHK

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Why is an _id with ObjectID added to when using MongoDB's $push to add new object to an array?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×