为什么在使用 MongoDB 的 $push 将新对象添加到数组时添加了带有 ObjectID 的 _id? [英] Why is an _id with ObjectID added to when using MongoDB's $push to add new object to an array?

查看:20
本文介绍了为什么在使用 MongoDB 的 $push 将新对象添加到数组时添加了带有 ObjectID 的 _id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Node.js 和 Mongoose.player 和锦标赛变量是 Mongoose 对象,在之前获取.

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

我想将一个新的锦标赛会话对象(不是猫鼬对象)添加到玩家对象的锦标赛会话字段中.我正在使用 findOneAndUpdate 来确保我不会两次添加相同的比赛(使用$ne")

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) {
            // ...
        });

一切正常,除了包含 ObjectID 的_id"字段被添加到锦标赛会话数组中新添加的会话中,我不明白为什么会发生这种情况.

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.

如果我手动添加_id"字段并且值为BLABLABLA",则永远不会存储_id"字段(因为它不应该,因为它没有在架构中声明)

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)

是的,这里是模式:

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

推荐答案

您可以通过使用自己的架构显式定义 tournamentSessions 数组来禁用 _id 字段,以便您可以将其 _id 选项 设置为 false:

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

这篇关于为什么在使用 MongoDB 的 $push 将新对象添加到数组时添加了带有 ObjectID 的 _id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆