Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basic commands in MongoDB

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article shows you what are the basic commands in MongoDB, which are concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

1. Three elements in MongoDB

Database

Set

Document

2. The data storage in MongoDB is stored in the form of Bson, and Bson is a binary json, so the form of record is similar to json data.

3. The data in the collection in MongoDB is different from that in the relational database, and the document structure in MongoDB can be different, so the scalability is very good.

Advantages of MongoDB: easy to extend, high performance, flexible data model

Disadvantages of MongoDB: repeated storage of data and large footprint

MongoDB start

Command line (terminal) start command

Mongod: running the server

Mongo: start the client

View help commands, default port, log location

Check out help: mongod-help

Check to see if the startup is successful: ps ajx | grep mongod

Default end: 27017

Location of the log: / var/log/mongodb/mongod.log

Mongodb client

Start the local client: mongo

Check out help: mongo-help

Exit: exit or ctrl+c

Basic commands for MongoDB

Operation database command

View the current database: db

View all databases: show dbs / show databases

Switch database: use db_name delete the current database: db.dropDatabase ()

Switch to a database that does not exist, and the added data will be created automatically

Show current database status: db.stats ()

Current database version: db.version ()

View the linked machine address of the current database: db.getMongo ()

Commands for manipulating collections

Do not manually create the collection:

When you add data to a collection that does not exist for the first time, the collection is created.

Create a combination manually:

Create user: db.createUser ({user: "laowang", pwd: "123456", roles: [{role: "userAdmin", db: "stu"}]})

Db.createCollection (name,options)

Db.createCollection ("stu")

Db.createCollection ("sub", {capped: true, size: 10})

Parameter capped: the default value is false: no upper limit is set, and the value true means upper limit is set.

Parameter size: when the capped value is true, you need to specify this parameter, which indicates the upper limit of bytes (in bytes)

When the file reaches the upper limit, the previous data will be overwritten, the earliest added data will be moved out, the rest will be moved up, and finally added to the last entry

Show all current users: show users

Delete user: db.removeUser ("userName")

View the collection: show collections

Delete the collection: db. Collection name .drop ()

Data types in mongo

Object ID: file ID

String: string, most common, must be a valid UTF-8

Boolean: stores a Boolean value, true or false

Integer: integers can be 32-bit or 64-bit, depending on the server

Double: storing floating-point values

Arrays: an array or list in which multiple values are stored in a key

Object: embedded file, that is, the value of each file is two files.

Null: storing null values

Timestamp: timestamp indicating the total number of seconds from 1970-1-1 to the present

Date: the UNIX time format in which the current period or time is stored

Note:

Create a period statement as follows: the format of the parameter is YYYY-MM-DD, and each file has an attribute of _ id, which ensures the uniqueness of each file.

New Date ('2017-12-20')

You can configure the _ id plug-in file. If it is not provided, MongoDB provides a unique _ id for each file, type: objectID

ObjectID is a 12-byte hexadecimal number (understand):

The first 4 bytes are the current timestamp

The next 3-byte machine ID

MongoDB's service process id in the next 2 bytes

The last three bytes are simple increments.

Insert data

Db. Collection name. Insert (document) when inserting a template file, if you do not specify the _ id parameter, MongoDB will assign a _ id of the only ObjectId type to the collection file.

Db.stu.insert ({name:'gj',gender:1})

Db.stu.insert ({_ id: "20170101", name:'gj',gender:1})

Insert a single entry as a dictionary and insert multiple entries as a list

Save

Db. Collection name .save (document)

Modify if the _ id of the backup file already exists, or add if the _ id of the backup file does not exist

Different from: insert if there is a direct error report

Simple query:

Db. Collection name. Find ()

Update

Grammar: db. Collection name .update (, {multi:})

Parameter query: query condition

Parameter update: update operator

Parameter multi: optional. Default is false, which means only the found section records are updated. A value of true means to update all the files with full conditions.

For example: note: "multi update only works with $operators" updates all, you must use $set

Db.stu.update ({name:'hr'}, {name:'mnc'}) updates one. Fields that are not updated will be discarded.

Db.stu.update ({name:'hr'}, {$set: {name:'hys'}}) updates a

Db.stu.update ({}, {$set: {gender:0}}, {multi:true}) updates all

Delete

Grammar: db. Collection name .remove (, {justOne:})

Parameter query: optional, conditions for deleted files

Parameter justOne: optional. If it is set to true or 1, only false bars will be deleted. Default is deleted, which means deleting multiple entries.

What are the basic commands in MongoDB? have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report