猫鼬-我的数据库复制属性,而不是将新条目与旧条目合并

时间:2019-03-18 22:19:24

标签: javascript database mongodb mongoose

在此处引用答案:https://stackoverflow.com/a/22680973/9817602

预期结果:将文档保存到数据库中时,所有文档都是唯一的。

策略:使用可一次注册文档然后逐步更新的功能。

问题:第一个记录创建一个新属性,但是该属性的下一个Model.save()创建一个相同的属性,而不是将值合并到同一属性中-例如,在我的模型中使用timePlot 9

尝试解决该问题

  • 使用不同的方法处理第一次保存的位置:
    • push(),
    • 直接分配我的值,例如property={}

当前结果-请注意timePlot:"9"的属性上的重复项:

     {
        "prospect": [
            {
                "$oid": "5c901344e3da317e2838482a"
            },
            {
                "$oid": "5c90134be3da317e2838482d"
            }
        ],
        "_id": {
            "$oid": "5c901338e3da317e28384828"
        },
        "timePlot": "9"
    },
    {
        "prospect": [
            {
                "$oid": "5c901338e3da317e28384826"
            }
        ],
        "_id": {
            "$oid": "5c901338e3da317e28384829"
        },
        "timePlot": "9"
    }

我的server.js代码段:

 let  scheduleInstance = new ScheduleMongoose(schedule) 
            let scheduleDocument = await ScheduleMongoose.findOne({day}, (err, data) =>{ 
                if(err) throw console.error(err)
                    // console.log("data ScheduleMongoose.findOne: ", data)
                return data 
            })
            if (scheduleDocument){ 
                // console.log("schedule found:", scheduleDocument);
                scheduleDocument.hour[daySlot].time.some(({timePlot}, i) =>{ 
                    if( timePlot=== hour[daySlot].time.timePlot) {
                        match=true;
                        console.log("timePlot match with time, updating timeProspectIndex...");
                        timeProspectIndex=i;
                        console.log("time prospect update");
                    }
                    return timePlot=== hour[daySlot].time.timePlot
                })
                scheduleInstance=scheduleDocument;
            }


             // time property exists => update object
            if( 
                match &&
                scheduleInstance.hour[daySlot].time.length>0 &&
                scheduleInstance.hour[daySlot].time[timeProspectIndex].prospect
            ){
                console.log("line 326 scheduleInstance.hour[daySlot].time[timeProspectIndex]: ",
                    scheduleInstance.hour[daySlot].time[timeProspectIndex]
                )


                console.log("\n object => inserting new prospect...")
                scheduleInstance.hour[daySlot].time[timeProspectIndex].prospect.push(prospect._id);
                console.log("object => insert new prospect succeed \n")   
            }  
            // new time property with sibling object => insert at end of array          
            else if (
                !match && 
                scheduleInstance.hour[daySlot].time.length>0 &&
                scheduleInstance.hour[daySlot].time[timeProspectIndex].length >0
            ){  
                    console.log(" \n adding new timeslot...") 
                    timeProspectIndex=scheduleInstance.hour[daySlot].time.length; 
                    scheduleInstance.hour[daySlot].time[timeProspectIndex]={
                        prospect: prospect._id,
                        timePlot: hour[daySlot].time.timePlot
                    } 
                    console.log("adding timeslot succeed \n")
            }
            // very new object in database => create property
            else{
                console.log("\n create time property...")
                let timeProspectIndex=0 

                let timeData={
                    prospect: prospect._id,
                    timePlot: hour[daySlot].time.timePlot
                } 
                scheduleInstance.hour[daySlot].time.push(timeData) 
                console.log("create time property succeed \n")
            }

***编辑:***当前我正在尝试以下操作:

 var schedule= {
    month,
    day,
    hour
 };
 var update ={
     prospect: prospect._id,
     timePlot: hour[daySlot].time.timePlot
 };

        console.log("line 353  _ schedule.hour[daySlot]: ",schedule.hour[daySlot])

        console.log("355 _ schedule.hour[daySlot].time: ", schedule.hour[daySlot].time)
        // console.log("bulkWrite: ScheduleMongoose", ScheduleMongoose)
        ScheduleMongoose.collection.bulkWrite([ 
            {
                updateOne:{
                    "filter": schedule.hour[daySlot].time.timePlot,  
                    "update":{
                        "$setOnInsert": { [schedule.hour[daySlot].time]:  update }
                    },
                    "upsert": true 
                }
            }, 
            {
                updateOne:{
                    "filter": schedule.hour[daySlot].time.timePlot, 
                    "update":{
                        "$addToSet": { [schedule.hour[daySlot].time]:  update }
                    },
                    "upsert": true 
                }
          }

        ]).then(res => {
            console.log("bulk succeed: ", res.insertedCount, res.modifiedCount, res.deletedCount);
            updateProspect(prospect, schedule)
        })
        .catch(err=>{ 
            return console.error(err) 
        })         

    }

返回我:

  

{MongoError:BSON字段“ update.updates.q”是错误的类型   'string',预期类型为'object'

我不知道出什么问题了, 任何提示都会很棒, 谢谢,

任何提示都会很棒, 谢谢

0 个答案:

没有答案