Mongodb一些记录

1.从_id中获取创建时间

把"_id"当作一个字符串来处理,然后从第10个字符开始,取8个字符,得到这条数据的创建时间戳(不带毫秒数)。在后面补上毫秒位数"000",然后用Date()方法构造成时间对象,赋值给createTime属性。

db.getCollection('fees').find({}).forEach(function(item){
          var _str = item._id.toString().substr(10, 8);
          var _date = new Date(Number(parseInt(_str, 16).toString() + '000'));
          item.createTime = _date;
          db.fees.save(item);
})

2.地理位置索引

mongodb有两种地图索引2dsphere和2d,针对不同的数据格式

对于2dsphere:
//数据类型
location : { 
      type : "Point" ,
      coordinates : [10,45] 
}
//创建
db.c.createIndex({'location':'2dsphere'})

支持方法:

操作方法实例备注
位置相交$geoIntersectsdb.c.map.find({‘location’:{‘geometry’:area}})area = {type: “Polygon”,coordinates: [ [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ] ]}
位置包含$withindb.c.map.find({‘location’:{‘geometry’:area}})
位置接近$neardb.c.map.find({‘location’:{‘geometry’:area}})
位置接近$nearSphere

或为2d索引:
//数据格式
location : [10,45]
//创建索引
db.c.createIndex( { "location" : "2d" })
//范围限制
//bits 精度1 ~ 32 
db.c.createIndex( { "location" : "2d" }, { min : -180 , max : 180,bits:26 })

支持方法:

操作方法实例备注
位置包含$withindb.c.map.find({‘location’:{‘$within’:[[10,10],[20,20]]})
矩形包含$boxdb.c.map.find({‘location’:{‘box’:[[10,10],[20,20]]}})
中心包含$centerdb.c.map.find({‘location’:{‘center’:[[20,20],5]}})
多边形包含$polygondb.c.map.find({‘location’:{‘polygon’:[[20,20],[10,10],[10,18],[13,21]]}})
位置接近$neardb.c.map.find({‘location’:{‘$near’:[10,20]})

3.删除指定字段

db.User.update({},{$unset:{'address':''}},false, true)

4.数据类型标注

Double1double
String2string
Object3object
Array4array
Binary data5binData
Undefined6undefined ܨ
ObjectId7objectId
Boolean8“bool”
Date9“date”
Null10“null”
Regular Expression11“regex”
DBPointer12“dbPointer”
JavaScript13“javascript”
Symbol14“symbol”
JavaScript(with scope)15“javascriptWithScope”
32bit integer16“int”
Timestamp17“timestamp”
64bit integer18“long”
Min key-1“minKey”
Max key127“maxKey”
find({"_id":{"$type":7}})

5.数组与嵌套查询

{ "_id" : ObjectId("5b1e31518726e33c3ce3386b"),"sname" : "刘家坊","tag_list" : [ "居民","村","石头","历史","寺庙"]}

1.查询带有“文物”标签的景点

db.getCollection('poi').find({"tag_list":"文物"})

2.查询同时包含“文物”与“寺庙”标签的景点($all)

db.getCollection('poi').find({"tag_list":{"$all":["文物","寺庙"]}})

3.查询tag_list中第一个标签为“民居“的景点(使用tag_list.index)

db.getCollection('poi').find({"tag_list.0":"民居"})

4.查询特定长度的数组

db.getCollection('poi').find({"tag_list":{"$size":5}})

5.数组嵌套查询
数据格式

"tag_list" : [
 {
   "tag_name" : "天空",
   "tag_confidence" : 79
 },
 {
   "tag_name" : "山",
   "tag_confidence" : 62
 },
 {
   "tag_name" : "云",
   "tag_confidence" : 49
 },
 {
   "tag_name" : "树木",
   "tag_confidence" : 26
 },
 {
   "tag_name" : "草地",
   "tag_confidence" : 15
}]

例如:需要查询标签“山”,并且标签的权重大于50的标签
错误写法:

db.getCollection('track_tag').find({"tag_list.tag_name":"山","tag_list.tag_confidence":{"$gt":5}});

以上查询语句查询的结果是标签中带有“山”,并且只要一个标签的权重大于5的标签被查处,而不是这个标签同时满足两个条件;
正确写法:

db.getCollection('track_tag').find({"tag_list":{"$elemMatch":{"tag_name":"山","tag_confidence":
{"$gte":463}}}})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值