mongobd commonly used, command

View the database

show dbs;

select a library

use db;

view the tables under the library (let's call it a table, called a table document in mongodb)

show collections;

insert data

db.table.insert( {'name':'demo' ,'sex':'m','age':18} );

(note the data type when inserting data)

Insert data can define the structure of each row at will, there is no fixed table structure in mongodb

eg: db.table. insert( {'names':'demo','sexs':'m','age':20} );

Query data

db.table.find();

(When querying conditional data, you should pay attention to The problem of data type)

eg:

db.table.insert( {'id':'123','age':18} );

db.table.insert( {'id':123 ,'age':20} );

db.table.find( {'id':123} ); # Only the second data can be

viewed Other conditions (the following usage):

$lt (less than), $lte (less than or equal to), $gt( greater than), $gte (greater than or equal to), $ne (not equal to)

db.table.find( {'age':{ '$lt':19 } } )

Summary of common commands in Mongodb

Sort


db.table.find().sort({'age':1} )

1 means ascending -1 means descending

Paging


db.table.find().limit(10).skip(20);

( skip specifies that several are ignored , the above query is similar to limit 20,10 in mysql)

Query statistics

db.table.count();

db.table.find({'age':{'$lt':19} } ).count()

delete data

db .table.remove();//Remove all

db.table.remove( {'id':123} );//Remove the specified data

to update

the data db.table.update( {'id':123},{'age ':25} )

// Find data with id 123 and update its age field to 25

do.table.update({'id':123} , {'age':22} ,true );

// Similar to The replace usage of mysql is updated if it exists, and it is added if it does not exist.

Index

db.table.ensureIndex({'id':1})//Create index

db.table.dropIndex({'id':1})//Delete index

db .table.ensureIndex({'id':1}, {unique:true}); // independent index

db.table.ensureIndex({'id':1,'age':1})//Joint index

db.table.ensureIndex( { loc : "2dsphere" } )//Spatial index

backup (use mongodump.exe to back up data )

mongodump.exe -d learn -o backup (backup entire learn database)

mongodump.exe -d learn -c test -o backup (backup individual tables)

( -d database -c datatables -o backup directory)

restore database ( Use mongorestore.exe to restore)

mongorestore.exe -d lear -c test backup/learn/unicorns.bson

(-d database -c data table)

export data

mongoexport.exe -d test -c mapinfo -o export.dat (export data It is json format data, the default format)

(-d database-c table-o export file name)

mongoexport.exe -d test -c mapinfo --csv -f id,name -o csv.csv (export data is csv format data )

( -d database -c table --csv export to csv format -f export field name -o export file name)

import data

mongoimport.exe -d test -c mapinfo mapinfo.dat (import json format data, default format)

(-d database -c table data source)

mongoimport.exe -d test -c mapinfo --type csv --headerline --file csv.csv (import csv format data)

(-d database-c table --type type --headerline do not import the first line --file data source)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326201826&siteId=291194637