dshfjsh_5455 2016-08-03 04:34
浏览 267
已采纳

计算数组中的元素数

I am creating an application, and it has a function which removes elements from an array inside a document in a MongoDB collection.

I need a way to unset the array's field as soon as the array becomes empty right after a remove operation. I need to do this so that I can differentiate between a document that has a non-empty array, and a document that has an empty array.

For example, right now my section collection looks like:

db.section.find({})

{ "_id" : ObjectId("57a0a38ad1c6ef24376477c5"), "sectionid" : "BTE4B", 
 "sectionname" : "BTech 4B", "year" : 4,
 "session" : 2016, "courseid" : "BTE-CS", "password" : "pm8xTE0-",
 "students" : 35, "addedon" : "2016-08-02 19:13:38", "teachers" : [ ] }

 { "_id" : ObjectId("57a0a96bd1c6ef24376477cd"), "sectionid" : "BTE4D",
 "sectionname" : "BTech 4D", "year" : 4, "session" : 2016, "courseid" :
 "BTE-CS", "password" : "sHhKr0Ov", "students" : 41, "addedon" :
 "2016-08-02 19:38:43", "teachers" : [ { "facultyid" : "CS-102",
 "subjectid" : "CS-ALGO" } ] }

I had applied a $pull operation using mgo on the teachers array of the first document, but I need a way to $unset it when it becomes empty.

Currently, I have managed to create a workaround in Go, but I would like to get away without any workarounds.

I hope I am not missing something very trivial here.

Thanks!

  • 写回答

2条回答 默认 最新

  • doudui9516 2016-08-03 08:37
    关注

    Would suggest using the findAndModify() API to do the $pull update operation first, this will return the modified document which you can then access the teachers array for checking. Once you check for array emptiness, you can then issue another update operation that calls the $unset operator on the teachers field if the array key has zero elements.

    A canonical example in mongo shell follows:

    change = db.section.findAndModify({
        query: { "sectionid": "BTE4D", "teachers.facultyid": "CS-102" },
        update: { "$pull": { "teachers": { "facultyid" : "CS-102" } } },
        new: true
    });
    
    printjson(change);
    
    if (!change.teachers[0]) {
         db.section.update(
            { "_id": change._id },
            { "$unset": { "teachers": "" } }
        );
    }
    
    db.section.find({ "_id": change._id })
    

    Implementing this with mgo, you would need the Query.Apply method which essentially wraps the findAndModify MongoDB command

    change := mgo.Change{
            Update: bson.M{"$pull": bson.M{"teachers": bson.M{"facultyid": "CS-102"}}},
            ReturnNew: true,
    }
    info, err = col.Find(M{"sectionid": "BTE4D"}).Apply(change, &doc)
    fmt.Println(doc.N)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料