美文网首页
Mongodb一些记录

Mongodb一些记录

作者: 谁在烽烟彼岸 | 来源:发表于2019-05-11 16:02 被阅读0次

    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'})
    

    支持方法:

    操作 方法 实例 备注
    位置相交 $geoIntersects db.c.map.find({‘location’:{‘geoIntersects’:{‘geometry’:area}}) area = {type: “Polygon”,coordinates: [ [ [ 0 , 0 ] , [ 3 , 6 ] , [ 6 , 1 ] , [ 0 , 0 ] ] ]}
    位置包含 $within db.c.map.find({‘location’:{‘within’:{‘geometry’:area}})
    位置接近 $near db.c.map.find({‘location’:{‘near’:{‘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 })
    

    支持方法:

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

    3.删除指定字段

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

    4.数据类型标注

    Double 1 double
    String 2 string
    Object 3 object
    Array 4 array
    Binary data 5 binData
    Undefined 6 undefined ܨ
    ObjectId 7 objectId
    Boolean 8 “bool”
    Date 9 “date”
    Null 10 “null”
    Regular Expression 11 “regex”
    DBPointer 12 “dbPointer”
    JavaScript 13 “javascript”
    Symbol 14 “symbol”
    JavaScript(with scope) 15 “javascriptWithScope”
    32bit integer 16 “int”
    Timestamp 17 “timestamp”
    64bit integer 18 “long”
    Min key -1 “minKey”
    Max key 127 “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}}}})
    

    相关文章

      网友评论

          本文标题:Mongodb一些记录

          本文链接:https://www.haomeiwen.com/subject/jacxaqtx.html