Basic usage skills of MongoDB and mongodb

Source: Internet
Author: User

Basic usage skills of MongoDB and mongodb

This section describes some basic operations of MongoDB.

Create user-specified database login get more command insert document MongDB common query comparison

Create user
> Db. createUser ({user: "<your user name>", pwd: "<your password>", roles: [{role: "readWrite", db: "<the database you want to create>"}]})
Specified database
# For example, if I have created an admin database, the switch mode is> use admin.

Note:Use the use command to specify the database used by Sanjin. If the database does not exist, a new database is created.

Login
> Db. auth ("<account>", "<password> ")

When the console output is1Indicates successful

Get more commands
> db.help()

More commands will be listed.

Insert document

MongoDB manages databases through collections. Similar to database tables, a Collection contains multiple documents, which are similar to the records of database tables. MongoDB does not need to create a set on display. You can add documents to the set directly. For example:

Db. test. insert ({_ id: "springboot", desc: "create distributed server", tag: ["IT", "Spring"],})
Introduction Parameters
_ Id: Fields starting with the line below MongoDB have special meanings, indicating the document's primary keyIf the document does not provide a primary key, the system automatically generates a primary key of the ObjectID type. Desc: Entry description tag: saved as an array. The MongDB field type can be string, number, boolean, date, or document type. Comment: This field is a JSON document. Comparison of common MongDB queries
MongoDB format Code Format Instance
$ Gt > "Comment. good": {$ gt: 1000} indicates that comment. good is an element greater than 1000.
$ Gte > = "Comment. good": {$ gte: 1000}
$ Lt < "Comment. good": {$ lt: 1000}
$ Lte <= "Comment. good": {$ lte: 1000}
$ Eq = "Comment. good": {$ eq: 1000}
$ Ne ! = "Comment. good": {$ ne: 1000}
$ In In "Tag": {$ in: ["IT", "template language"] Find All tags that contain IT and template languages}
$ Nin Not in Not in

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.