Mongodb基础知识

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. 批量插入

mongodb.exe 后执行

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

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()
       }
    )                  
};

5. mapduce

db.getCollection("").mapReduce(function () {
	emit(this.key, this.value);
}, function (key, values) {
	return values[0];
}, {
    out: {
        inline: 1
    },
    verbose: true
});

6. cmd 运行

net start Mongodb

net stop Mongodb

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值