猫鼬不会更新一个集合,但会更新其他集合

时间:2020-06-23 20:42:00

标签: node.js mongodb mongoose

我正在使用猫鼬来操纵我的MongoDB。我在更新我的收藏集中的任何文档时遇到麻烦。我有三个集合,在其中两个集合中,我可以操纵值,但在一个集合中,我不能操纵值。这是一个有效的集合的架构:

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const playerSchema = new Schema({
...
});

const Player = mongoose.model('Player', playerSchema);

module.exports = Player;

这是集合的架构。

const mongoose = require('mongoose')

const Schema = mongoose.Schema;

const crewSchema = new Schema({
    gm: {
        type: String
    },
    characters: {
        type: Array
    },
...
});
const Crew = mongoose.model('Crew', crewSchema);

module.exports = Crew;

这是一个findOneAndUpdate,适用于集合:

Character.findOneAndUpdate({'_id': charId}, {'crewId': crewId});

这是我尝试更新文档内部的数组。我知道它很乱,但是我尝试了$push,但是它没有用,所以我尝试了。

Crew.findById(crewId)
        .then(crew => {
            console.log(crew)
            // arrayChars = crew.characters
            arrayChars.push(charId)
            console.log(arrayChars)
            Crew.updateOne({ '_id': crewId}, {'characters': arrayChars});
            Character.findOneAndUpdate({'_id': crewId}, {'gm': crewId});
        })

任何帮助将不胜感激!

0 个答案:

没有答案