【问题标题】:MongoDB $push is not actually pushing anything onto the arrayMongoDB $push 实际上并没有将任何东西推送到数组上
【发布时间】:2020-07-01 01:06:42
【问题描述】:

我用// This Command Does not work 注释了这行代码,我怀疑它正在中断。在 mongoose 的调试日志中,输出如下所示: 但是 DaysOfWeek 模式的 Monday 对象中的 MedicineIds 数组中没有添加任何内容。

以下是 DayOfWeek.findOneAndUpdate() 的调试输出,我在其中推回数组,但在我的 mongo 数据库中看不到结果。

Mongoose: dayofweeks.insertOne({ medicineIds: [], _id: 'Monday', __v: 0 }, { session: null }) // <- response to $push

Mongoose: medicines.insertOne({ times: [ 1, 2 ], dayNames: [ 'Monday' ], _id: ObjectId("5e73d816d54b1202e15bb96b"), nam
e: 'Provolone', count: 23, __v: 0 }, { session: null })
Mongoose: dayofweeks.findOne({ _id: 'Monday' }, { projection: {} })

变异

const Mutation = new GraphQLObjectType({
    name: 'Mutation',
    fields: {
        addDayOfWeek: {
            type: DayOfWeekType,
            args: {
                name: { type: new GraphQLNonNull(GraphQLString) }
            },
            resolve(parent, args) {
                let dayOfWeek = new DayOfWeek({
                    _id: args.name,
                    medicineIds: new Array()
                });

                return dayOfWeek.save(); 
            }
        },
        addNewMedicine: {
            type: MedicineType,
            args: {
                name: { type: new GraphQLNonNull(GraphQLString) },
                count: { type: new GraphQLNonNull(GraphQLInt) },
                times: { type: new GraphQLNonNull(GraphQLList(GraphQLInt))},
                dayNames: { type: new GraphQLNonNull(GraphQLList(GraphQLString))}
            },

            resolve (parent, args) {
                let medicine = new Medicine({
                    name: args.name,
                    count: args.count,
                    times: args.times,
                    dayNames: args.dayNames
                }); 
                args.dayNames.forEach((dayId) => {
                    DayOfWeek.findOneAndUpdate( // This Command Does Not Work: 
                        // medicine._id, dayId are correct at this point of the 
                        //code
                        { _id: dayId }, 
                        { $push: { medicineIds: medicine._id }},
                        { new: true, useFindAndModify: false }
                    );

                });
                return medicine.save(); 
            }
        }
    }
});

DayOfWeek 架构

const mongoose = require('mongoose');
const Schema = mongoose.Schema; 

const dayOfWeekSchema = new Schema({
    _id: String,
    medicineIds: [String] // I am trying to push onto this array
});

module.exports = mongoose.model('DayOfWeek', dayOfWeekSchema);

医学架构

const mongoose = require('mongoose');
const Schema = mongoose.Schema; 

const medicineSchema = new Schema({
    id: String,
    count: Number,
    name: String,
    times: [Number],
    dayNames: [String]
});

module.exports = mongoose.model('Medicine', medicineSchema);

【问题讨论】:

  • 在这一行:{ $push: { medicineIds: medicine._id }},如果medicine对象还没有被保存,是否有_id字段?
  • { times: [ 1, 2 ], dayNames: [ 'Monday' ], _id: 5e73d190b925d602667fa85c, name: 'Provolone', count: 23 } Monday 5e73d190b925d602667fa85c 是 console.log(medicine) 的输出,所以是的。我会检查这是否是所保存药物的实际 ID。即使那样,dayOfWeek 的列表也应该收到一个 ID,即使它可能不是正确的,对吧?
  • 我看不到 console.log 的调用位置。通常,如果您没有为 _id 显式设置值,则该字段在您保存对象之前不会存在。
  • 尝试在medicine.save() 之后添加findOneAndUpdate,即类似medicine.save().then(() =&gt; { args.dayNames.forEach( ...update code... ) }
  • 它正在退出函数然后没有履行承诺,因为 findOneAndUpdate 返回一个承诺,所以我不得不等待所有承诺的数组。

标签: node.js mongodb mongoose graphql


【解决方案1】:
 await Promise.all(args.dayNames.map(dayName => {
                    return DayOfWeek.findOneAndUpdate({ _id: dayName }, { $push: { medicineIds: medicine._id }});
                })).catch((err) => console.error(err)); 

                return await medicine.save(); 

我只是这样做了,它有效。唔。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-29
    • 1970-01-01
    • 2018-09-24
    • 2018-10-26
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多