mongodb

##数据库数据存放目录
dbpath=/data/mongodb27017/data
#数据库日志存放目录
logpath=/data/mongodb27017/logs/mongodb.log
#以追加的方式记录日志
logappend=true
#端口号 默认为27017
port=27017
#以后台方式运行进程
fork=true
#使用较小的默认文件
smallfiles=true
#开启用户认证
#auth=true
#关闭http接口,默认关闭http端口访问
#nohttpinterface=true
#mongodb所绑定的ip地址
#bind_ip = 127.0.0.1
#这个选项可以过滤掉一些无用的日志信息,若需要调试使用请设置为false
quiet=true

> show dbs;
> show databases;
> use databaseName;
> show collections;
> show tables;
隐示创建数据库
> use db1;
> db.createCollection('coll_1'); ##将隐示创建数据库db1,显示创建集合coll_1
隐示创建集合
> db.coll_2.insert({name:'neo',age:25}); ##将隐示创建集合coll_2,插入文档{name:neo,age:25}
查看集合中的所有文档 db.collectionName.find( json )
> db.coll_2.find()
{ "_id" : ObjectId("5a50d5ad78bfd9c3d03d0440"), "name" : "neo", "age" : 25 }
删除集合
> db.coll_1.drop();
true
> db.coll_2.drop();
true
删除数据库
> use db1;
switched to db db1
> db.dropDatabase();
{ "dropped" : "db1", "ok" : 1 }


CURD操作
insert: db.collectionName.insert({ json })

> use db1;
switched to db db1
> db.createCollection('coll_1');
{ "ok" : 1 }
> db.coll_1.insert({name:'neo',age:25}); ##单行插入
WriteResult({ "nInserted" : 1 })
> db.coll_1.insert({_id:1,name:'chen',address:['xg','wh'],detail:{math:[85,90,95],english:[75,80,85] }}); ##指定_id插入
WriteResult({ "nInserted" : 1 })
> db.coll_1.insert([{_id:2,name:'aaa',age:20},{name:'bbb',sex:'m'},{name:'ccc',address:'wh'}]); ##多行插入
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
> db.coll_1.count(); ##查询集合中一共有多少行 db.collectionName.count( json )
5
> db.coll_1.find();
{ "_id" : ObjectId("5a50d7c278bfd9c3d03d0441"), "name" : "neo", "age" : 25 }
{ "_id" : 1, "name" : "chen", "address" : [ "xg", "wh" ], "detail" : { "math" : [ 85, 90, 95 ], "english" : [ 75, 80, 85 ] } }
{ "_id" : 2, "name" : "aaa", "age" : 20 }
{ "_id" : ObjectId("5a50da3478bfd9c3d03d0442"), "name" : "bbb", "sex" : "m" }
{ "_id" : ObjectId("5a50da3478bfd9c3d03d0443"), "name" : "ccc", "address" : "wh" }


delete:db.collectionName.remove({ json },[true]) ##如果带有true,表示一次只删除一行

> use db1;
switched to db db1
> show collections;
coll_1
> db.coll_1.find();
{ "_id" : ObjectId("5a50d7c278bfd9c3d03d0441"), "name" : "neo", "age" : 25 }
{ "_id" : 1, "name" : "chen", "address" : [ "xg", "wh" ], "detail" : { "math" : [ 85, 90, 95 ], "english" : [ 75, 80, 85 ] } }
{ "_id" : 2, "name" : "aaa", "age" : 20 }
{ "_id" : ObjectId("5a50da3478bfd9c3d03d0442"), "name" : "bbb", "sex" : "m" }
{ "_id" : ObjectId("5a50da3478bfd9c3d03d0443"), "name" : "ccc", "address" : "wh" }
> db.coll_1.remove({name:'ccc',address:'wh'}); ##删除查询到的所有的行
WriteResult({ "nRemoved" : 1 })
> db.coll_1.find();
{ "_id" : ObjectId("5a50d7c278bfd9c3d03d0441"), "name" : "neo", "age" : 25 }
{ "_id" : 1, "name" : "chen", "address" : [ "xg", "wh" ], "detail" : { "math" : [ 85, 90, 95 ], "english" : [ 75, 80, 85 ] } }
{ "_id" : 2, "name" : "aaa", "age" : 20 }
{ "_id" : ObjectId("5a50da3478bfd9c3d03d0442"), "name" : "bbb", "sex" : "m" }
> db.coll_1.remove({}); ##删除所有行
WriteResult({ "nRemoved" : 4 })
> db.coll_1.find();

原文地址:https://www.cnblogs.com/chenqs/p/8233195.html