当前位置:   article > 正文

Mongodb基础知识_mongodb主键id策略

mongodb主键id策略

Mongodb基础知识

1. 基本数据类型

1. null

null 用于表示空值或者不存在的字段

{“x”:null}

2. 布尔型

布尔类型有两个值 true 和 false

{“x”: true}

3. 数值

shell 默认使用64 位浮点型数值。因此,以下数值在shell中是很正常的

{“x”: 3.14} {“x”: 3}

对于数值型,可使用NumberInt (表示4字节带符号整数)

或NumberLong类(表示8字符符号整数)

分别举例如下

{“x”: NumberInt(“3”)} {“x”: NumberLong(“3”)}

4. 字符串

UTF-8 字符串都可以表示为字符串类型的数据

{“x”: “foobar”}

5. 日期

日期被存储为自新纪元以来的毫秒数,不存储时区:

关于日期时间是按照 javascript 的风格来的

{“x”:new Date()}

6. 正则表达式

查询时,使用正则表达式作为限定条件,语法也与JavaScript的正则表达式语法相同:

{“x”: /foobar/}

7. 数组

数据列表或数据集可以表示为数组

{“x”:[“a”,“b”,“c”]}

8. 内嵌文档

文档可嵌套其他文档,被嵌套的文档作为父文档的值:

{“x”:{“foo”:“bar”}}

9. 对象id

对象id是一个12字节的Id,是文档的唯一标识。

1234567891011

时间戳+机器码+PID+计数器

{“x”:ObjectId()}

10. 其他类型

二进制数据和代码

2. shell 小贴士

1. help

help

db.help() 数据库级别的帮助

db.foo.help() 查看集合级别的帮助

3. 操作文档命令

        db.foo.find().help() - show DBCursor help
        db.foo.bulkWrite( operations, <optional params> ) - bulk execute write operations, optional parameters are: w, wtimeout, j
        db.foo.count( query = {}, <optional params> ) - count the number of documents that matches the query, optional parameters are: limit, skip, hint, maxTimeMS
        db.foo.countDocuments( query = {}, <optional params> ) - count the number of documents that matches the query, optional parameters are: limit, skip, hint, maxTimeMS
        db.foo.estimatedDocumentCount( <optional params> ) - estimate the document count using collection metadata, optional parameters are: maxTimeMS
        db.foo.convertToCapped(maxBytes) - calls {convertToCapped:'foo', size:maxBytes}} command
        db.foo.createIndex(keypattern[,options])
        db.foo.createIndexes([keypatterns], <options>)
        db.foo.dataSize()
        db.foo.deleteOne( filter, <optional params> ) - delete first matching document, optional parameters are: w, wtimeout, j
        db.foo.deleteMany( filter, <optional params> ) - delete all matching documents, optional parameters are: w, wtimeout, j
        db.foo.distinct( key, query, <optional params> ) - e.g. db.foo.distinct( 'x' ), optional parameters are: maxTimeMS
        db.foo.drop() drop the collection
        db.foo.dropIndex(index) - e.g. db.foo.dropIndex( "indexName" ) or db.foo.dropIndex( { "indexKey" : 1 } )
        db.foo.hideIndex(index) - e.g. db.foo.hideIndex( "indexName" ) or db.foo.hideIndex( { "indexKey" : 1 } )
        db.foo.unhideIndex(index) - e.g. db.foo.unhideIndex( "indexName" ) or db.foo.unhideIndex( { "indexKey" : 1 } )
        db.foo.dropIndexes()
        db.foo.explain().help() - show explain help
        db.foo.reIndex()
        db.foo.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.
                                                      e.g. db.foo.find( {x:77} , {name:1, x:1} )
        db.foo.find(...).count()
        db.foo.find(...).limit(n)
        db.foo.find(...).skip(n)
        db.foo.find(...).sort(...)
        db.foo.findOne([query], [fields], [options], [readConcern])
        db.foo.findOneAndDelete( filter, <optional params> ) - delete first matching document, optional parameters are: projection, sort, maxTimeMS
        db.foo.findOneAndReplace( filter, replacement, <optional params> ) - replace first matching document, optional parameters are: projection, sort, maxTimeMS, upsert, returnNewDocument
        db.foo.findOneAndUpdate( filter, <update object or pipeline>, <optional params> ) - update first matching document, optional parameters are: projection, sort, maxTimeMS, upsert, returnNewDocument
         db.foo.getDB() get DB object associated with collection
        db.foo.getPlanCache() get query plan cache associated with collection
        db.foo.getIndexes()
        db.foo.insert(obj)
        db.foo.insertOne( obj, <optional params> ) - insert a document, optional parameters are: w, wtimeout, j
        db.foo.insertMany( [objects], <optional params> ) - insert multiple documents, optional parameters are: w, wtimeout, j
        db.foo.mapReduce( mapFunction , reduceFunction , <optional params> )
        db.foo.aggregate( [pipeline], <optional params> ) - performs an aggregation on a collection; returns a cursor
        db.foo.remove(query)
        db.foo.replaceOne( filter, replacement, <optional params> ) - replace the first matching document, optional parameters are: upsert, w, wtimeout, j
        db.foo.renameCollection( newName , <dropTarget> ) renames the collection.
        db.foo.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection name
        db.foo.save(obj)
        db.foo.stats({scale: N, indexDetails: true/false, indexDetailsKey: <index key>, indexDetailsName: <index name>})
        db.foo.storageSize() - includes free space allocated to this collection
        db.foo.totalIndexSize() - size in bytes of all the indexes
        db.foo.totalSize() - storage allocated for all data and indexes
        db.foo.update( query, <update object or pipeline>[, upsert_bool, multi_bool] ) - instead of two flags, you can pass an object with fields: upsert, multi, hint, let
        db.foo.updateOne( filter, <update object or pipeline>, <optional params> ) - update the first matching document, optional parameters are: upsert, w, wtimeout, j, hint, let
        db.foo.updateMany( filter, <update object or pipeline>, <optional params> ) - update all matching documents, optional parameters are: upsert, w, wtimeout, j, hint, let
        db.foo.validate( <full> ) - SLOW
        db.foo.getShardVersion() - only for use with sharding
        db.foo.getShardDistribution() - prints statistics about data distribution in the cluster
        db.foo.getSplitKeysForChunks( <maxChunkSize> ) - calculates split points over all chunks and returns splitter function
        db.foo.getWriteConcern() - returns the write concern used for any operations on this collection, inherited from server/db if set
        db.foo.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the collection
        db.foo.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the collection
        db.foo.latencyStats() - display operation latency histograms for this collection
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

1. 批量插入

mongodb.exe 后执行

for(var i = 0; i < 1000000; i ++) {
   db.foo.insert({"name":"zhangsan" + i, "age" : i});
}
  • 1
  • 2
  • 3

2. 更新操作命令

  1. $set 设置新值
  2. $inc 新增数值
  3. $push 向已有的数组末加入一个元素,要是没有就创建一个元素
  4. $addToSet

3. 写入安全机制

  1. 应答式写入安全机制

4. 索引

1. 批量插入1000000 数据

for(i=0;i<1000000;i++){      
    db.users.insert(
       {
        "i":i,"username":"user"+i,
        "age":Math.floor(Math.random()*120),
        "created":new Date()
       }
    )                  
};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

5. mapduce

db.getCollection("").mapReduce(function () {
	emit(this.key, this.value);
}, function (key, values) {
	return values[0];
}, {
    out: {
        inline: 1
    },
    verbose: true
});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

6. cmd 运行

net start Mongodb

net stop Mongodb

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/648243
推荐阅读
相关标签
  

闽ICP备14008679号