Mongodb入门

MongoDB基础 12/31 阅读 2339 views次 人气 374
摘要:

Mongo DB,是目前非常流行的一种非关系型数据库(NoSql)。

1. 下载: 
https://fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plus-2.6.0.zip
2. 解压: 
D:\ProgramFiles\mongodb-win32-x86_64-2008plus-2.6.0
3. 环境变量: 
path=D:\ProgramFiles\mongodb-win32-x86_64-2008plus-2.6.0\bin
4. 数据文件存放目录: 
D:\ProgramData\mongodb
5. 启动: 
mongod --dbpath=D:\ProgramData\mongodb
6. Windows服务:
注册->
mongod --dbpath=D:\ProgramData\mongodb --logpath=D:\ProgramData\mongodb\logs\mongodb.log --install 
mongod --dbpath=D:\ProgramData\mongodb --logpath=D:\ProgramData\mongodb\logs\mongodb.log --install --serviceName MongoDB
mongod --dbpath=D:\ProgramData\mongodb --logpath=D:\ProgramData\mongodb\logs\mongodb.log --install --serviceName MongoDB --serviceUser admin --servicePassword 123124
移除注册->
mongod --dbpath=D:\ProgramData\mongodb --logpath=D:\ProgramData\mongodb\logs\mongodb.log --reinstall
mongod --dbpath=D:\ProgramData\mongodb --logpath=D:\ProgramData\mongodb\logs\mongodb.log --remove
默认端口->
27017  
Window注册->
sc create MongoDB binPath= "\"D:\ProgramFiles\mongodb-win32-x86_64-2008plus-2.6.0\bin\mongod.exe\" --service --config=\"D:\ProgramData\mongodb\config\mongod.cfg\"" DisplayName= "MongoDB 2.6 Standard" start= "auto"  
mongod.cfg文件内容->
dbpath=D:\ProgramData\mongodb
logpath=D:\ProgramData\mongodb\logs\mongodb.log
7. 客户端连接:
操作->
C:\Users\admin>mongo
MongoDB shell version: 2.6.0
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user
8. 客户端GUI工具:
MongoVUE->http://www.mongovue.com/
RockMongo->http://code.google.com/p/rock-php/
MongoHub->https://github.com/bububa/MongoHub
9. 数据操作:
插入操作->
C:\Users\admin>mongo
MongoDB shell version: 2.6.0
connecting to: test
> j = { name : "mongo" };
{ "name" : "mongo" }
> db.things.save(j);
WriteResult({ "nInserted" : 1 })
>
> i = { id : "001" };
{ "id" : "001" }
> db.things.save(i);
WriteResult({ "nInserted" : 1 })
>
> db.things.find();
{ "_id" : ObjectId("535f448a3033c0d034af957d"), "name" : "mongo" }
{ "_id" : ObjectId("535f44b13033c0d034af957e"), "id" : "001" }
>
插入操作->
> for( var i = 1; i < 10; i++ ) db.things.save( { x:4, j:i } );
WriteResult({ "nInserted" : 1 })
>
> db.things.find();
{ "_id" : ObjectId("535f448a3033c0d034af957d"), "name" : "mongo" }
{ "_id" : ObjectId("535f44b13033c0d034af957e"), "id" : "001" }
{ "_id" : ObjectId("535f45383033c0d034af957f"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("535f45383033c0d034af9580"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("535f45383033c0d034af9581"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("535f45383033c0d034af9582"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("535f45383033c0d034af9583"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("535f45383033c0d034af9584"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("535f45383033c0d034af9585"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("535f45383033c0d034af9586"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("535f45383033c0d034af9587"), "x" : 4, "j" : 9 }
查询操作->
> var cursor = db.things.find();
> while (cursor.hasNext()) printjson(cursor.next());
{ "_id" : ObjectId("535f448a3033c0d034af957d"), "name" : "mongo" }
{ "_id" : ObjectId("535f44b13033c0d034af957e"), "id" : "001" }
{ "_id" : ObjectId("535f45383033c0d034af957f"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("535f45383033c0d034af9580"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("535f45383033c0d034af9581"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("535f45383033c0d034af9582"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("535f45383033c0d034af9583"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("535f45383033c0d034af9584"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("535f45383033c0d034af9585"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("535f45383033c0d034af9586"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("535f45383033c0d034af9587"), "x" : 4, "j" : 9 }
>
> db.things.find().forEach(printjson);
{ "_id" : ObjectId("535f448a3033c0d034af957d"), "name" : "mongo" }
{ "_id" : ObjectId("535f44b13033c0d034af957e"), "id" : "001" }
{ "_id" : ObjectId("535f45383033c0d034af957f"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("535f45383033c0d034af9580"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("535f45383033c0d034af9581"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("535f45383033c0d034af9582"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("535f45383033c0d034af9583"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("535f45383033c0d034af9584"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("535f45383033c0d034af9585"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("535f45383033c0d034af9586"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("535f45383033c0d034af9587"), "x" : 4, "j" : 9 }
>
> var cursor = db.things.find();
> printjson(cursor[4]);
{ "_id" : ObjectId("535f45383033c0d034af9581"), "x" : 4, "j" : 3 }
>
> var arr = db.things.find().toArray();
> arr[5];
{ "_id" : ObjectId("535f45383033c0d034af9582"), "x" : 4, "j" : 4 }
>
查询操作->
> db.things.find({name:"mongo"}).forEach(printjson);
{ "_id" : ObjectId("535f448a3033c0d034af957d"), "name" : "mongo" }
>
> db.things.find({x:4}).forEach(printjson);
{ "_id" : ObjectId("535f45383033c0d034af957f"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("535f45383033c0d034af9580"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("535f45383033c0d034af9581"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("535f45383033c0d034af9582"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("535f45383033c0d034af9583"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("535f45383033c0d034af9584"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("535f45383033c0d034af9585"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("535f45383033c0d034af9586"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("535f45383033c0d034af9587"), "x" : 4, "j" : 9 }
>
> db.things.find({x:4}, {j:true}).forEach(printjson);
{ "_id" : ObjectId("535f45383033c0d034af957f"), "j" : 1 }
{ "_id" : ObjectId("535f45383033c0d034af9580"), "j" : 2 }
{ "_id" : ObjectId("535f45383033c0d034af9581"), "j" : 3 }
{ "_id" : ObjectId("535f45383033c0d034af9582"), "j" : 4 }
{ "_id" : ObjectId("535f45383033c0d034af9583"), "j" : 5 }
{ "_id" : ObjectId("535f45383033c0d034af9584"), "j" : 6 }
{ "_id" : ObjectId("535f45383033c0d034af9585"), "j" : 7 }
{ "_id" : ObjectId("535f45383033c0d034af9586"), "j" : 8 }
{ "_id" : ObjectId("535f45383033c0d034af9587"), "j" : 9 }
>
> printjson(db.things.findOne({name:"mongo"}));
{ "_id" : ObjectId("535f448a3033c0d034af957d"), "name" : "mongo" }
>
> db.things.find().limit(3);
{ "_id" : ObjectId("535f448a3033c0d034af957d"), "name" : "mongo" }
{ "_id" : ObjectId("535f44b13033c0d034af957e"), "id" : "001" }
{ "_id" : ObjectId("535f45383033c0d034af957f"), "x" : 4, "j" : 1 }
>
修改操作->
> db.things.update({name:"mongo"},{$set:{name:"mongo_new"}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> 
> db.things.find();
{ "_id" : ObjectId("535f448a3033c0d034af957d"), "name" : "mongo_new" }
{ "_id" : ObjectId("535f44b13033c0d034af957e"), "id" : "001" }
{ "_id" : ObjectId("535f45383033c0d034af957f"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("535f45383033c0d034af9580"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("535f45383033c0d034af9581"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("535f45383033c0d034af9582"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("535f45383033c0d034af9583"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("535f45383033c0d034af9584"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("535f45383033c0d034af9585"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("535f45383033c0d034af9586"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("535f45383033c0d034af9587"), "x" : 4, "j" : 9 }
>
删除操作->
> db.things.remove({name:"mongo_new"});
WriteResult({ "nRemoved" : 1 })
>
> db.things.find();
{ "_id" : ObjectId("535f44b13033c0d034af957e"), "id" : "001" }
{ "_id" : ObjectId("535f45383033c0d034af957f"), "x" : 4, "j" : 1 }
{ "_id" : ObjectId("535f45383033c0d034af9580"), "x" : 4, "j" : 2 }
{ "_id" : ObjectId("535f45383033c0d034af9581"), "x" : 4, "j" : 3 }
{ "_id" : ObjectId("535f45383033c0d034af9582"), "x" : 4, "j" : 4 }
{ "_id" : ObjectId("535f45383033c0d034af9583"), "x" : 4, "j" : 5 }
{ "_id" : ObjectId("535f45383033c0d034af9584"), "x" : 4, "j" : 6 }
{ "_id" : ObjectId("535f45383033c0d034af9585"), "x" : 4, "j" : 7 }
{ "_id" : ObjectId("535f45383033c0d034af9586"), "x" : 4, "j" : 8 }
{ "_id" : ObjectId("535f45383033c0d034af9587"), "x" : 4, "j" : 9 }
>
10. 高级查询
<, <=, >, >= 
db.collection.find({ "field" : { $gt: value } } ); // 大于: field > value
db.collection.find({ "field" : { $lt: value } } ); // 小于: field < value
db.collection.find({ "field" : { $gte: value } } ); // 大于等于: field >= value
db.collection.find({ "field" : { $lte: value } } ); // 小于等于: field <= value
db.collection.find({ "field" : { $gt: value1, $lt: value2 } } ); // value1 < field < value
$all 匹配所有
db.users.find({age : {$all : [6, 8]}}); // $all 必须满足[ ]内的所有值

$exists 判断字段是否存在
// 查询所有存在age 字段的记录
db.users.find({age: {$exists: true}});
// 查询所有不存在name 字段的记录
db.users.find({name: {$exists: false}});
Null 值处理
db.c2.find({age:{"$in":[null], "$exists":true}})

$mod 取模运算
// 查询age 取模10 等于0 的数据
db.student.find( { age: { $mod : [10,1]}})
// 查询age 取模6 等于1 的数据
db.c1.find({age: {$mod : [ 6 ,1 ]}})
$ne 不等于
// 查询x 的值不等于3 的数据
db.things.find( { x : { $ne : 3}});
// 查询age 的值不等于7 的数据
db.c1.find( { age : { $ne : 7 }});
$in 包含
// 查询x 的值在2,4,6 范围内的数据
db.things.find({x:{$in: [2,4,6]}});
$nin 不包含
// 查询x 的值在2,4,6 范围外的数据
db.things.find({x:{$nin: [2,4,6]}});
// 查询age 的值在7,8 范围外的数据
db.c1.find({age:{$nin: [7,8]}});
$size 数组元素个数
// 查询age 的值在7,8 范围外的数据
db.c1.find({age:{$nin: [7,8]}});
正则表达式匹配
// 查询不匹配name=B*带头的记录
db.users.find({name: {$not: /^B.*/}});
 

评论

该文章不支持评论!

分享到: