Download and install
1. Official website address:www.mongodb.com/try/downloa…
2. Click the “DOWNLOAD(TGZ)” button, rename the decompressed file to /usr/local and put it in /usr/local. By default, /usr/local is not available in Finder. To see the hidden directory, open Finder, press Shift + Command +G, type /usr/local, and press Enter.
3. Create a path, otherwise Mongod cannot execute it
- Open the current user environment variable configuration file: Enter in terminal
sudo vi ~/.bash_profile
Copy the code
- Add your installation path to the document
export PATH=$PATH:/usr/local/mongodb/bin
, save.
In English input state, press the letter I to edit; Press Esc and enter :wq to save the configuration and exitCopy the code
- Put the code you just entered into effect:
source .bash_profile
Copy the code
Pay attention to If it is to use item2 + oh – my – ZSH, you will need to set the environment variable (export PATH = $PATH: / usr/local/mongo/bin) PATH added to the root directory ~ /. ZSHRC file. Step up
Verify that the installation is successful
The input terminal
mongod -version
Copy the code
If this version of good proof has been installed successfully
- Catalina, the latest version of the Mac system, has a surprising change: it does not allow you to change the root directory (which is also discussed in the forum topics) :
% sudo mkdir -p /data/db
mkdir: /data/db: Read-only file system
Copy the code
Unfortunately, this is not explicitly stated in Apple’s Catalina release notes, other than a brief mention in the Catalina feature:
MacOS Catalina runs on a dedicated read-only system volume
Since the directory /data/db is encoded as the MongoDB default directory, one workaround is to specify a directory where dbpath is not in the root directory. Such as:
Mongod --dbpath ~/data/db //~ indicates the user directoryCopy the code
This puts MongoDB data in your home directory. Just make sure that the path ~/data/db does exist. Create two directories data>db if they do not exist
Open your browser and type localhost:27017.
It looks like you are trying to access MongoDB over HTTP on the native driver port.
Reopen another command line and type
mongo
Copy the code
You can perform operations on the database. If you want to stop MongoDB, you must exit correctly. Otherwise, the next time you connect to the database, there will be a problem.
use admin;
db.shutdownServer();
Copy the code
5. Visualization tools
RoboMongo is a cross-platform MongoDB GUI client management tool that supports Windows, MacOS, and Linux. It features support for SSL connections to MongoDB servers as well as SSH tunnel connections. The RoboMongo query interface supports tree view, table view and text view, and can also save the query results for future use. Robomongo can choose its own version to download.
Author: keyuan0214 links: www.jianshu.com/p/eb4c36ff4… The copyright of the book belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please indicate the source.
Attached MongoDB database operation common commands
- Show DBS Displays all databases
- Use dbname Uses a database
- Db Displays the current database
- Create and use the database use dbname
- DropDatabase db.dropdatabase ()
- Db.createcollection (name,options) Options {capped(fixed size):true,autoIndexId(_id set to default index):true,size(and capped ):6142800, Max (maximum number of documents):1000}
- Drop collection db.collectionName.drop()
- Db.collectionname.insert (document)
- Db.collectionname.find ()
- Db.collectionname. update(query condition, update content, optional parameter),db.coolectionName.save(document)
eg:db.users.update({a:1},{$set:{a:11}})
- Delete document db.collectionName.remove(Query condition, optional)
Eg :db.users.remove({a:1},false) Delete all documents in the document where a is 1
- Db.collectionname.find ()/ db.collectionname.find ().pretty()
- Db.users. find({age:{$LTE :10}}) Age is less than or equal to 10
- Db.users. find({age:{$ne:10}})age does not equal 10
- Db. The users. The find ({$or: [{age: 10}, {} age: 20]}) values of the age of 10 or 20
- Db.collectionname.find ().limit(n) Limits the number of entries to be displayed
- Db.collectionname.find ().skip(n) Skips a specified amount of data
Eg: db.users.find().skip(5).limit(5) display items 6-10
- CollectionName. Sort ({key:1}) 1 indicates the sequence. -1 indicates the descending order
- The index db. CollectionName. CreateIndex ({key: 1})