A quick guide to show you how to do basic operations like create, update, find, delete record and indexing in MongoDB. This example is using MongoDB 2.0.7, running on Mac OS X 10.8, both MongoDB client and server console are run on localhost, same machine.
1. Install MongoDB
Mongodb Osx
Brew tap mongodb/brew. If you have already done this for a previous installation of MongoDB, you can skip this step. To install MongoDB, run the following command in your macOS Terminal application: brew install mongodb-community@5.0. Alternatively, you can specify a previous version of MongoDB if desired.
- Mac OS and iOS library for MongoDB and BSON. Contribute to paulmelnikow/ObjCMongoDB development by creating an account on GitHub.
- Use this tutorial to manually install MongoDB 5.0 Enterprise Edition on macOS using a downloaded.tgz tarball. MongoDB Enterprise Edition is available on select platforms and contains support for several features related to security and monitoring.
Install MongoDB on Windows, Ubuntu or Mac OS X. The installation is easy, basically just download the MongoDB zip file, extra and run the command – $MongoDB-folder/bin/mongod
.
Uses mongod
to start MongoDB.
2. Connect MongoDB
To connect MongoDB, uses $MongoDB-folder/bin/mongo
3. Create a database or table (collection)
In MongoDB, both database and table are created automatically when the first time data is inserted. Uses use database-name
Kontakt 5.6.8 add library. , to switch to your database (even this is not created yet).
In below example, after you inserted a single record, database “mkyong”, and table “users” are created on the fly.
Mongodb On Mac
Three database commands you should know.
show dbs
– List all databases.use db_name
– Switches to db_name.show collections
– List all tables in the current selected database.
In MongoDB, collection means table in SQL.
4. Insert A Record
To insert a record, uses db.tablename.insert({data})
or db.tablename.save({data})
, both works, no idea why MongoDB created both.
5. Update A Record
To update a record, uses db.tablename.update({criteria},{$set: {new value}})
. In below example, the password of username : “mkyong” is updated.
6. Find Records
To find or query records, uses db.tablename.find({criteria})
.
6.1 List all records from table “users”.
6.2 Find records where username is “google”
6.3 Find records where username’s length is less than or equal to 2
6.4 Find records where username field is existed.
7. Delete Record
To delete a record, uses db.tablename.remove({criteria})
. In below example, the record of username “google” is deleted.
To delete all records from a table, uses
db.tablename.remove()
.To drop the table, uses
db.tablename.drop()
.8. Indexing
Index may help you increase the speed of querying data.
8.1 List all indexes of table “users”, by default the column “_id” is always the primary key and created automatically.
8.2 To create an index, uses db.tablename.ensureIndex(column)
. In below example, an index is created on column “username”.
8.3 To drop an index, uses db.tablename.dropIndex(column)
. In below example, the index on column “username” is deleted or dropped.
8.4 To create an unique index, uses db.tablename.ensureIndex({column},{unique:true})
. In below example, an unique index is created on column “username”.
10. Help
At last, uses help()
to guide you how to do things in MongoDB.
10.1 help
– All available commands.
10.2 db.help()
– Shows help on db.
10.3 db.collection.help()
– Shows help on collection (table).
10.4 db.collection.function.help()
– Shows help on function.
Done. Hope this summary of MongoDB commands could help others.
References
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.