Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

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

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
    }
}));
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
}));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...