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条)

报告相同问题?

悬赏问题

  • ¥100 iOS开发关于快捷指令截屏后如何将截屏(或从截屏中提取出的文本)回传给本应用并打开指定页面
  • ¥15 unity连接Sqlserver
  • ¥15 图中这种约束条件lingo该怎么表示出来
  • ¥15 VSCode里的Prettier如何实现等式赋值后的对齐效果?
  • ¥15 流式socket文件传输答疑
  • ¥20 keepalive配置业务服务双机单活的方法。业务服务一定是要双机单活的方式
  • ¥50 关于多次提交POST数据后,无法获取到POST数据参数的问题
  • ¥15 win10,这种情况怎么办
  • ¥15 如何在配置使用Prettier的VSCode中通过Better Align插件来对齐等式?(相关搜索:格式化)
  • ¥100 在连接内网VPN时,如何同时保持互联网连接