mongo学习笔记

mongo学习笔记

一 安装测试配置

1 下载安装
    从mongodb官网下载对应的压缩包,解压即可。
    copy命令到/usr/local/bin目录 (cp mongod /usr/local/bin;cp mongo /usr/local/bin)
2 创建配置文件
     参考官方文档https://docs.mongodb.com/manual/reference/configuration-options/#configuration-file
     
 

常用配置:

    systemLog:
       destination: file
       path: "/var/log/mongodb/mongod.log"
       logAppend: true
    storage:
       dbPath: "/var/mongoData"
       journal:
          enabled: true
    processManagement:
       fork: true
    net:
       bindIp: 127.0.0.1
       port: 27017
    setParameter:
       enableLocalhostAuthBypass: false
3 启动服务
    mongod --config=/etc/mongodb/mongodb.conf
4 打开客户端
    mongo
5 关闭mongo
    use admin
    db.shutdownServer()

二 CURD的操作

show dbs 显示所有数据库
use user 使用user数据库(不存在就会自动创建,插入一些值后才会显示)
db 查看当前使用的数据库
show collections 显示该库下所有集合
db.userinfo.insert({"a":1,"b":1,"fr":[1,2,3,4]}) 在user数据库userinfo集合中插入数据
db.dropDatabase() 删除数据库
db.userinfo.remove({}) 清空集合
db.userinfo.drop() 删除集合

SELECT a,b FROM users   <==>   db.users.find({}, {"a" : 1, "b" : 1});
SELECT * FROM users WHERE age=33    <==>   db.users.find({"age" : 33});
SELECT a,b FROM users WHERE age=33  <==>   db.users.find({"age" : 33}, {"a" : 1, "b" : 1});
SELECT a,b FROM users WHERE age=33 ORDER BY name    <==>   db.users.find({"age" : 33}, {"a" : 1, "b" : 1}).sort({"name" : 1});
SELECT * FROM users WHERE age>33    <==>   db.users.find({"age" : {'$gt' : 33}});
SELECT * FROM users WHERE age<33    <==>   db.users.find({"age" : {'$lt' : 33}});
SELECT * FROM users WHERE name LIKE "%Joe%" <==>   db.users.find({"name" : /Joe/});
SELECT * FROM users WHERE name LIKE "Joe%"  <==>   db.users.find({"name" : /^Joe/});
SELECT * FROM users WHERE age>33 AND age<=40    <==>   db.users.find({"age" : {'$gt' : 33,'$lte' : 40}});
SELECT * FROM users ORDER BY name DESC  <==>   db.users.find().sort({"name" : -1});
CREATE INDEX myindexname ON users(name) <==>   db.users.ensureIndex({"name" : 1});
CREATE INDEX myindexname ON users(name,ts DESC) <==>   db.users.ensureIndex({"name" : 1, "ts" : -1});
SELECT * FROM users WHERE a=1 and b='q' <==>   db.users.find({"a" : 1, "b" : "q"});
SELECT * FROM users LIMIT 10 SKIP 20    <==>   db.users.find().limit(10).skip(20);
SELECT * FROM users WHERE a=1 or b=2    <==>   db.users.find({'$or' : {"a" : 1, "b" : 2}});
SELECT * FROM users LIMIT 1 <==>   db.users.find().limit(1);
EXPLAIN SELECT * FROM users WHERE z=3   <==>   db.users.find({"z" : 3}).explain()
SELECT DISTINCT last_name FROM users    db.runCommand({"distinct" : "users", "key" : "last_name"});
SELECT COUNT(*y) FROM users <==>   db.users.count();
SELECT COUNT(*y) FROM users where AGE > 30  <==>   db.users.find({"age" : {'$gt' : 30}}).count();
SELECT COUNT(AGE) from users    <==>   db.users.find({"age" : {'$exists' : true}}).count();
UPDATE users SET a=1 WHERE b='q'    <==>   db.users.update({"b" : "q"}, {'$set' : {"a" : 1}});
UPDATE users SET a=a+2 WHERE b='q'  <==>   db.users.update({"b" : "q"}, {'$inc' : {"a" : 2}});
DELETE FROM users WHERE z="abc" <==>   db.users.remove({"z" : "abc"});

php 操作mongo记录,请参考
http://blog.csdn.net/qq135554…
https://my.oschina.net/u/8121…

    原文作者:hufeng
    原文地址: https://segmentfault.com/a/1190000006822732
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞