记录一下使用过程中的收集的一些技巧。

字符串是否包含指定子串

(substring为子串)
db.usercollection.find({name:{$regex: .*substring.*}})

字符串字段值长度

db.usercollection.find({name: {$type: 2}, $where: "this.name.length > 40"}).limit(2);


db.usercollection.find({name: {$exists: true}, $where: "this.name.length > 40"}).limit(2);

regex
db.usercollection.find({"name": {"$type": 2, "$regex": /^.{41,}$/}}).limit(2);

数组是否包含指定的数据

(tags为数组字段,usercollection为指定collection)
db.usercollection.find({tags: {$in: ['a']}})

labix.org/v2/mgo(未测试)
C.Find(bson.M{"tags": "a"}).All(&usercollection)
c.Find(bson.M{"tags": bson.M{"$in": []string{"a"}}}).All(&usercollection)

删除重复数据的简单方法

1.导出数据:
mongoexport.exe -d database_name -c collection_name -o filename.json

2.清空当前collection:
db.usercollection.remove({})

3.新建唯一索引:
db.usercollection.createIndex({public_no:1}, {unique:true})

4.导入数据:
mongoimport -d database_name -c collection_name --upsert filename.json