MongoDB Installation and Basic Operation
1. Install
Enter / usr / local
cd /usr/local
download
sudo curl -O https://fastdl.mongodb.org/osx/mongodb-osx-x86_64-3.4.2.tgz
Decompression
sudo tar -zxvf mongodb-osx-x86_64-3.4.2.tgz
Rename mongodb
sudo mv mongodb-osx-x86_64-3.4.2 mongodb
Configuration environment variable
export PATH=/usr/local/mongodb/bin:$PATH
2. Start
sudo mongodb
3. Basic Operation
Go to the bin directory, interact
cd /usr/local/mongodb/bin
./mongo
Create a database
use DATABASE_NAME
View all databases
show dbs
Create a collection (table)
db.createCollection(name, options)
Query existing collection (table)
show collections
Inserting data (JSON format)
newMsg collection insert data in the test database
sudo ./mongoimport --db test --collection newMsg --file ~/Desktop/ptapp/newMsg.json
Complement:
1. Insert or json format data files need to exit Mongo , returns to the bin directory operation
2. When importing data -collection corresponding set of name must -file consistent collection under the name of the path, otherwise the following error occurs, it is easily overlooked pit
error validating settings: incompatible options: --file and positional argument(s)
Query data
NewMsg table query on to: "p_u_b2dc9fe03d4d90" data:
db.newMsg.find({to: "p_u_b2dc9fe03d4d90"}).sort({ "_id": 1}).skip(0).limit(30)
db.newMsg.find({ "to":'p_u_b2dc9fe03d4d90'}).pretty()
Nested array of query
db.newMsg.aggregate([
{$match: {"payload.bodies.msg": '付款了怎么反还?'}},
{$unwind: "$payload"},
{$unwind: "$payload.bodies"},
{$match: {"payload.bodies.msg": '付款了怎么反还?'}},
]);
db.newMsg.aggregate([
{$match: {"msg_id": '571378421368621020'}},
]);
Complement: This nested query directly, only to return, is to check the msg, then check in accordance with msg_id
Export data to be found in the specified file
-h host
-p 端口号
-d 数据库名称
-c 表名称
-q 查询条件语句
~/ 进入根目录
sudo ./mongoexport -h localhost -p 27017 -d test -c 2016 -q "{to: 'p_u_b1d692e437499f'}" -o ~/Desktop/findResult/from_2016.json
Delete collection (table)
db.createCollection.drop()
On here, more operations can directly view the MongoDB Tutorial