In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains some basic operations of MongoDB, the content is clear and clear, interested friends can learn, I believe it will be helpful after reading.
Introduction to MongoDB:
MongoDB is an open source database system written by C++ language. MongoDB stores the data as a document. MongoDB is a database based on distributed file storage. MongoDB provides a document-oriented storage, easy to operate and easy to set any property in the MongoDB record index Mongo supports rich query expressions. Query instructions use tags in the form of JSON to easily query objects embedded in the document and
Array. MongoDB supports the startup of multiple storage engines: wireTiger, MMaPv1, and other MongoDB servers:
Use the mongod command to start the server
Mongodb commonly used startup parameter-bind_ip: bind service IP. If 127.0.0.1 is bound, it can only be accessed locally. Default local IP--port is not specified: specify service port number. Default port 27017--logpath: specify the MongoDB log file storage path-- dbpath: specify the database path [need to be specified to start successfully]-- serviceName: specify the service name [mainly used to specify the name when installing the service]-- serviceDisplayName: specify the service name, which is executed when there are multiple mongodb services. [mainly used to specify a name when installing a service]
For more parameter information, type:
Mongod-- help or mongod-h runs the MongoDB server as a Windows service:
The above startup mode requires a window to be suspended.
If you don't want to hang a window all the time, you can add these startup parameters to the service and start mongod as a service, so you don't have to go to so much trouble.
Examples of input commands are as follows:
Mongod-- dbpath "D:\ data\ db"-- logpath "D:\ data\ log\ mongodb.log"-- serviceName "mongodb"-- serviceDisplayName "mongodb"-- install installation service must be parameter introduction:-- install: indicate installation into service-- serviceName: specify service name-- serviceDisplayName: specify service name, other settings are optional when there are multiple mongodb services, confirm whether to enter it according to your own needs.
In this way, you only need to start the mongod service when using it.
MongoDB client connection:
Use the mongo command to connect to the server.
Mongo [options] [db address] [file names (ending in .js)] the local server can use: mongo or mongo localhost remote can use: mongo IP address
To get more parameter setting information, enter the following command:
Explanation of mongo-help or mongo-hSQL and MongoDB related concepts:
The explanation of this related concept is to enable some people who have SQL learning experience (those who do not have should be able to understand it) to understand the structure of MongoDB faster.
What is BSON:BSON () is a json-like binary storage format, referred to as Binary JSON, which, like JSON, supports embedded document objects and array objects, but BSON has some data types that JSON does not have, such as Date and BinData types. BSON can be used as a storage form of network data exchange, which is somewhat similar to Google's Protocol Buffer, but BSON is a storage form of schema-less. Its advantage is high flexibility, but its disadvantage is that the space utilization is not very ideal. BSON has three characteristics: lightweight, ergodic, efficient example of BSON: {"name": "alex", "age": 18} data types supported by BSON: database operation: display all databases [note] The database with empty data is not displayed by default]: show dbs displays the current database object or collection: db switches the database, if the database does not exist, create the database and then switch to the specified database: use database name create database: use database name database name can be any character, but cannot contain empty string, period (.), or "." The default database is test, if you did not create a new database, the collection will be stored in the test database to delete the database: db.dropDatabase (): delete the current database, it is recommended to use the db command to confirm the current database collection operation: the collection in mongodb is schemeless, mongodb and there are no strict constraints on the insertion of data, the structure of documents stored in the collection can be different. The following two documents can be saved in a collection at the same time: {"name": "alex"} {"age": 18, "sex": "man"}
Name of the collection: the name of the collection must begin with a letter or an underscore. The collection name protects the numeric collection name from the dollar character "$", which is the system reserved character. The name of the collection cannot exceed 128 characters. In addition, "." The use of symbols is allowed in collections, and they are called Subcollection sets. Create a collection: db.createCollection (name, {size:..., capped:..., max:}) name is the collection name size,capped,max is optional: size represents the collection size, capped represents whether to limit the collection size (set by size), max represents the maximum number of documents for the collection there are many options, for more information, you can refer to the official documentation, such as storageEngine,collation. View all collections of the current database: show collections delete collections: db. Collection name. Drop () modifies the collection name: db. Collection name. RenameCollection () gets collection help information: db. Collection name. Help () document operation: insert document: db. The collection name. Insert (document): document is in BSON format. Db.users.insertOne ({name: "sue", age: 19title status: "P"}) db. The collection name. InsertOne (document): document is in BSON format. Db.teacher.insert ({name: "sue"}) db.teacher.insert ([{"name": "Lili"}, {"name": "Alex"}]) db. Collection name .insertMany ([document,document,document.]) : document is a BSON format. Db.users.insertMany ([{name: "bob", age: 42, status: "A",}, {name: "ahn", age: 22, status: "A",}, {name: "xi", age: 34, status: "D",}]) View data: db. Set name. Find (,) db. Collection name. Findone (,): return only one document query filter can be as follows: {}: for all documents returned, db. The collection name. Find ({}), which is equivalent to db. Collection name. Find () db.teacher.find () {key1:value1,key2:value2... }: returns the document of key1==value1 and key2==value2 Db.teacher.find ({name: "alex"}) db.teacher.find ({name: "jack", course: "linux"}) {: {:},...}: operator can have $lt less than and $gt greater than $gte is greater than or equal to, $lte is less than or equal to, $ne is not equal to db.class.find ({"member": {$gt:5}}) db.class.find ({"member": {$gt:5}, grade: {$gt:3}}) query filter multiple conditions and and or: and by default, multiple conditions are separated by commas if you want to use or: {$or [{:}, {:}, {:} …] } combination of and and or: ({and condition, $or: [or condition]}) db.teacher.find ({course: "linux", $or: [{name: "Lili"}, {name: "Alex"}]}) the condition of query filter can also be type checking: use $type for type checking, the type number detected by type, according to the value in the above BSON type table. Filter out whether the field data type is string: $type:1 filter out whether the field data type is string; $type:2 filter out whether the field data type is Boolean; $type:8 filter out whether the field data type is null: $type:10... .. Other if you want to view the document db in a formatted way. Collection name. Find (). Pretty () Common format: pretty format: update document: update document can use the following: db. Collection name. UpdateOne (, {option}): update a single document db. Collection name. UpdateMany (, {option}): update multiple documents db. Collection name .update (, {option}): update the document. Optional single document or multiple documents query is in BSON format. As a condition for finding documents, the condition reference of query indicates how to modify the document and which fields to modify. The query filterupdate in the above search document is in BSON format. To modify the field value, you need to add {$set: {"key": "newvalue"}, otherwise {key: "newvalue"} will replace the original document, resulting in the loss of data in other fields. Option:upsert is whether to insert new data if there is no data to be modified. The default is that false does not insert db. The multi in the collection name .update () defaults to false, which updates only the first document data found, and if changed to true, updates all matching documents. WriteConcern is the level at which an exception is thrown. Delete data: db. Collection name. Remove (query): delete all eligible documents query: the condition of the deleted document. If the condition is {}, all documents are deleted, and query can refer to the query filterjustOne in the lookup document: (optional) if set to true or 1, only one document is deleted. WriteConcern: (optional) the level at which the exception is thrown.
For more information, you can refer to the official documentation. Https://docs.mongodb.com/manual/reference/bson-types/
After reading the above content, do you have a further understanding of some basic operations of MongoDB? if you want to learn more, 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.