1、 查询语句
左边是mongodb查询语句,右边是sql语句。对照着用,挺方便。
Mongo查询语句 | SQL语句 |
---|---|
db.users.find() | select * from users |
db.users.find({“age” : 27}) | select * from users where age = 27 |
db.users.find({“username” : “joe”, “age” : 27}) | select * from users where “username” = “joe” and age = 27 |
db.users.find({}, {“username” : 1, “email” : 1}) | select username, email from users |
db.users.find({}, {“username” : 1, “_id” : 0}) // no case // 即使加上了列筛选,_id也会返回;必须显式的阻止_id返回 | select username from users |
db.users.find({“age” : {"$gte" : 18, “$lte” : 30}}) | select * from users where age >=18 and age <= 30 // $lt(<) $lte(<=) $gt(>) $gte(>=) |
db.users.find({“username” : {"$ne" : “joe”}}) | select * from users where username <> “joe” |
db.users.find({“ticket_no” : {"$in" : [725, 542, 390]}}) | select * from users where ticket_no in (725, 542, 390) |
db.users.find({“ticket_no” : {"$nin" : [725, 542, 390]}}) | select * from users where ticket_no not in (725, 542, 390) |
db.users.find({"$or" : [{“ticket_no” : 725}, {“winner” : true}]}) | select * form users where ticket_no = 725 or winner = true |
db.users.find({“id_num” : {"$mod" : [5, 1]}}) | select * from users where (id_num mod 5) = 1 |
db.users.find({"$not": {“age” : 27}}) | select * from users where not (age = 27) |
db.users.find({“username” : {"$in" : [null], “$exists” : true}}) | select * from users where username is null // 如果直接通过find({“username” : null})进行查询,那么连带"没有username"的纪录一并筛选出来 |
db.users.find({“name” : /joey?/i}) // 正则查询,value是符合PCRE的表达式 | |
db.food.find({fruit : {$all : [“apple”, “banana”]}}) // 对数组的查询, 字段fruit中,既包含"apple",又包含"banana"的纪录 | |
db.food.find({“fruit.2” : “peach”}) // 对数组的查询, 字段fruit中,第3个(从0开始)元素是peach的纪录 | |
db.food.find({“fruit” : {"KaTeX parse error: Expected 'EOF', got '}' at position 10: size" : 3}̲}) // 对数组的查询, 查…size前面无法和其他的操作符复合使用 | |
db.users.findOne(criteria, {“comments” : {“KaTeX parse error: Expected 'EOF', got '}' at position 12: slice" : 10}̲}) // 对数组的查询,只返…slice” : -10}, {"$slice" : [23, 10]}; 分别返回最后10条,和中间10条 | |
db.people.find({“name.first” : “Joe”, “name.last” : “Schmoe”}) // 嵌套查询 | |
db.blog.find({“comments” : {“KaTeX parse error: Expected '}', got 'EOF' at end of input: …", "score" : {"gte” : 5}}}}) // 嵌套查询,仅当嵌套的元素是数组时使用, | |
db.foo.find({"KaTeX parse error: Expected 'EOF', got '}' at position 33: …+ this.y == 10"}̲) // 复杂的查询,where当然是非常方便的,但效率低下。对于复杂查询,考虑的顺序应当是 正则 -> MapReduce -> $where | |
db.foo.find({"$where" : “function() { return this.x + this.y == 10; }”}) // $where可以支持javascript函数作为查询条件 | |
db.foo.find().sort({“x” : 1}).limit(1).skip(10); // 返回第(10, 11]条,按"x"进行排序; 三个limit的顺序是任意的,应该尽量避免skip中使用large-number |
查询语句语法:
MongoDB查询语句(转)
MongoDB 查询文档
参考:
MongoDB简单查询语句
2、 MongoDB服务启动问题
安装mongod之后配置/etc/mongod.conf如下:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# Where and how to store data.
storage:
# dbPath: /var/lib/mongodb
dbPath: /opt/database/mongodb
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
通过service mongod start启动服务报错:
[root@localhost log]# systemctl status mongod
● mongod.service - High-performance, schema-free document-oriented database
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Thu 2019-06-06 10:30:25 CST; 8s ago
Docs: https://docs.mongodb.org/manual
Process: 7338 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 7339 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 7340 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 7341 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=1/FAILURE)
6月 06 10:30:25 localhost.localdomain systemd[1]: Starting High-performance, schema-free document-oriented database...
6月 06 10:30:25 localhost.localdomain mongod[7341]: about to fork child process, waiting until server is ready for connections.
6月 06 10:30:25 localhost.localdomain mongod[7341]: forked process: 7343
6月 06 10:30:25 localhost.localdomain mongod[7341]: ERROR: child process failed, exited with error number 1
6月 06 10:30:25 localhost.localdomain systemd[1]: mongod.service: Control process exited, code=exited, status=1/FAILURE
6月 06 10:30:25 localhost.localdomain systemd[1]: mongod.service: Failed with result 'exit-code'.
6月 06 10:30:25 localhost.localdomain systemd[1]: Failed to start High-performance, schema-free document-oriented database.
到/var/log/mongodb目录下查看日志,发现目录不存在:原来mongod服务不能自行创建目录,手工创建目录后直接执行命令行启动mongod,仍旧报错:
[root@localhost log]# mkdir mongodb
[root@localhost log]# chown mongod:mongod mongodb
[root@localhost log]# /usr/bin/mongod -f /etc/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 7485
ERROR: child process failed, exited with error number 100
vi /var/log/mongodb/mongod.log
2019-06-06T10:33:31.122+0800 I STORAGE [initandlisten] exception in initAndListen: 29 Data directory /opt/database/mongodb not found., terminating
创建数据目录后再次执行,成功:
[root@localhost lxca]# mkdir -p /opt/database/mongodb
[root@localhost mongodb]# /usr/bin/mongod -f /etc/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 7633
child process started successfully, parent exiting
手工杀掉进程后通过服务方式启动:
[root@localhost mongodb]# ps -ef | grep mongo
root 7633 1 0 10:36 ? 00:00:00 /usr/bin/mongod -f /etc/mongod.conf
root 7670 934 0 10:36 pts/1 00:00:00 grep --color=auto mongo
[root@localhost mongodb]# kill -9 7633
[root@localhost mongodb]# ps -ef | grep mongo
root 7687 934 0 10:37 pts/1 00:00:00 grep --color=auto mongo
[root@localhost mongodb]# service mongod start
Redirecting to /bin/systemctl start mongod.service
Job for mongod.service failed because the control process exited with error code.
See "systemctl status mongod.service" and "journalctl -xe" for details.
[root@localhost mongodb]# systemctl status mongod
● mongod.service - High-performance, schema-free document-oriented database
Loaded: loaded (/usr/lib/systemd/system/mongod.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Thu 2019-06-06 10:37:33 CST; 20s ago
Docs: https://docs.mongodb.org/manual
Process: 7718 ExecStartPre=/usr/bin/mkdir -p /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 7719 ExecStartPre=/usr/bin/chown mongod:mongod /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 7720 ExecStartPre=/usr/bin/chmod 0755 /var/run/mongodb (code=exited, status=0/SUCCESS)
Process: 7721 ExecStart=/usr/bin/mongod $OPTIONS (code=exited, status=1/FAILURE)
6月 06 10:37:33 localhost.localdomain systemd[1]: Starting High-performance, schema-free document-oriented database...
6月 06 10:37:33 localhost.localdomain mongod[7721]: about to fork child process, waiting until server is ready for connections.
6月 06 10:37:33 localhost.localdomain mongod[7721]: forked process: 7723
6月 06 10:37:33 localhost.localdomain mongod[7721]: ERROR: child process failed, exited with error number 1
6月 06 10:37:33 localhost.localdomain systemd[1]: mongod.service: Control process exited, code=exited, status=1/FAILURE
6月 06 10:37:33 localhost.localdomain systemd[1]: mongod.service: Failed with result 'exit-code'.
6月 06 10:37:33 localhost.localdomain systemd[1]: Failed to start High-performance, schema-free document-oriented database.
查看/var/log/mongodb/mongod.log,虽然日志文件存在,但是没有新的日志内容,查看权限:
[root@localhost mongodb]# ll
总用量 8
-rw-r--r-- 1 root root 4983 6月 6 10:36 mongod.log
发现文件所属用户/组是root,删除后重新执行:
[root@localhost mongodb]# rm -rf mongod.log
[root@localhost mongodb]# service mongod start
Redirecting to /bin/systemctl start mongod.service
Job for mongod.service failed because the control process exited with error code.
See "systemctl status mongod.service" and "journalctl -xe" for details.
虽然仍旧失败,但是日志正常生成:
2019-06-06T10:39:22.528+0800 I STORAGE [initandlisten] exception in initAndListen: 20 Attempted to create a lock file on a read-only directory: /opt/database/mongodb, terminating
修改目录属主后仍旧失败:
[root@localhost database]# chown mongod:mongod mongodb
2019-06-06T10:41:16.818+0800 I STORAGE [initandlisten] exception in initAndListen: 20 Attempted to create a lock file on a read-only directory: /opt/database/mongodb, terminating
删除mongodb目录下的内容后仍旧失败,但不是数据文件的问题了:
[root@localhost database]# cd mongodb/
[root@localhost mongodb]# rm -rf *
2019-06-06T10:42:48.102+0800 E NETWORK [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock Operation not permitted
删除sock文件后,终于成功:
[root@localhost tmp]# rm -rf /tmp/mongodb-27017.sock
[root@localhost mongodb]# service mongod start
Redirecting to /bin/systemctl start mongod.service
[root@localhost mongodb]# ps -ef | grep mongo
mongod 8122 1 1 10:43 ? 00:00:00 /usr/bin/mongod -f /etc/mongod.conf
root 8143 934 0 10:44 pts/1 00:00:00 grep --color=auto mongo
总结:
- mongo数据库的日志目录和数据文件目录需要先创建好,并且属主为mongod:mongod
- 一般错误都可以通过查看日志找到(日志在/etc/mongod.conf中配置),如果日志找不到或者没有更新,需要首先解决日志问题。
- 日志问题:一是目录需要存在,二是目录属主需要是mongod:mongod,如果日志文件已经存在,则日志文件属主也需要是mongod:mongod
- SELinux:打开SELinux会导致相应目录没有权限导致错误( [initandlisten] exception in initAndListen: 20 Attempted to create a lock file on a read-only directory: /opt/database/mongodb, terminating)