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

后端 未结 1 959
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 11:40

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 Mongo

相关标签:
1条回答
  • 2020-11-30 12:33

    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
    }));
    
    0 讨论(0)
提交回复
热议问题