#db.collection_name.createIndex({field_name: 1 or -1})
db.index_basic.createIndex({ts_code:1})
db.index_basic.getIndexes()
db.index_basic.dropIndex({ts_code:1})
db.index_basic.dropIndexes() # can not drop default `_id` index
The value 1 is for ascending order and -1 is for descending order
# export db to outdb on node1 mongodump --db myfb --out outdb zip outdb.zip -r outdb/ scp outdb.zip myfb:~/
# import db on node2 unzip outdb.zip mongorestore outdb
# check db file size du -h outdb.zip 742M
du -h outdb 9.1G
# check to see mongo > show dbs; myfb 3.1G
mongo gui
Robomongo offically changed it’s name and released two different products Studio 3T and Robo 3T. Old robomongo is now called Robo 3T. Studio 3T is for professionals.
wget https://download-test.robomongo.org/linux/robo3t-1.3.1-linux-x86_64-7419c406.tar.gz
vim .bashrc
export PATH=/home/kezunlin/program/robo3t/bin:$PATH
allow mongodb to access from remote.
vim /etc/mongodb.conf
#bind_ip = 127.0.0.1
bind_ip = 0.0.0.0
by default, mongodb only allow to access from local.
restart mongodb again
> sudo service mongodb status
mongodb.service - An object/document-oriented database
Loaded: loaded (/lib/systemd/system/mongodb.service; enabled; vendor preset: enabled)
Active: active (running) since 四 2019-09-26 16:11:03 CST; 7s ago
Docs: man:mongod(1)
Main PID: 4917 (mongod)
Tasks: 10
Memory: 3.0G
CPU: 70ms
CGroup: /system.slice/mongodb.service
└─4917 /usr/bin/mongod --config /etc/mongodb.conf
9月 26 16:11:03 node17 systemd[1]: Started An object/document-oriented database.
9月 26 16:11:03 node17 mongod[4917]: warning: bind_ip of 0.0.0.0 is unnecessary; listens on all ips by default
access from remote now
robo3t
python mongodb
pip install pymongo
pip install mongoengine
One library that provides a higher abstraction on top of PyMongo is MongoEngine. MongoEngine is an object document mapper (ODM), which is roughly equivalent to a SQL-based object relational mapper (ORM). The abstraction provided by MongoEngine is class-based, so all of the models you create are classes.
deftest_mongo_engine(): # define collection classPost(Document): title = StringField(required=True, max_length=200) content = StringField(required=True) author = StringField(required=True, max_length=50) published = DateTimeField(default=datetime.datetime.now)
connect('mydb', host='localhost', port=27017, alias="default"# must be `default` ) # mongoengine.connection.MongoEngineConnectionError: You have not defined a default connection
post_1 = Post( title='Sample Post', content='Some engaging content', author='Scott' ) post_1.save() # This will perform an insert print(post_1.title) print(post_1.id)
post_1.title = 'A Better Post Title' post_1.save() # This will perform an atomic edit on "title" print(post_1.title) print(post_1.id)