doudou8081 2015-01-02 12:38
浏览 521

GOlang mongodb从集合中删除所有项目

How to remove all items from collection stored in mongodb using GO lang?

In mongo console I can use:

db.mycollection.remove({})

where empty brackets {} mean all document pattern.

In GO lang (I use "gopkg.in/mgo.v2" and "gopkg.in/mgo.v2/bson") there are methods:

sess.DB("mydb").C("mycollection").Remove(...)
or
sess.DB("mydb").C("mycollection").RemoveAll(...)

but both of them needs parameter that implements selector. For example selector can be a bson map

bson.M{"id": id}

but I want to remove all elements, not a particular one.

  • 写回答

2条回答 默认 最新

  • douhun8647 2015-01-02 12:53
    关注

    See the MongoDB documentation at: http://docs.mongodb.org/manual/tutorial/remove-documents/

    To remove all the documents of a given collection, just call RemoveAll with an empty selector. Just passing nil as a parameter should work fine:

    sess.DB("mydb").C("mycollection").RemoveAll(nil)
    

    Be sure to check the returned objects though.

    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了