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

MongoDB Database-basic Syntax

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

First, the characteristics of MongoDB database and the characteristics of installing MongoDB database are document-oriented, mode free json data model (bson) (can be understood as a dictionary) multi-level index high availability replication set horizontal expansion cross-platform, multi-language interface weak transaction type big data, high concurrency, weak transaction web2.0 Internet application. Advantages: weak consistency, document format storage, built-in gridFS (distributed file system) disadvantages: do not support transactions, large space-consuming MongoDB database installation: click next all the way Finish configuration environment variable: configure the bin directory into the environment variable to execute: cmd > mongod-v (test whether the environment variable is configured) create the following folder (file path is as follows:) 1 New data folder 2 New log folder 3 New mongo.conf File 4 New mongo.cfg File (under log folder) File path: Server |-bin |-file path Data |-log |-mongo.cfg |-mongo.conf edit the mongo.conf file (add the following) dbpath=C:\ Program Files\ MongoDB\ Server\ 3.6\ data # Database path software installation directory (mine is the default directory) logpath=C:\ Program Files\ MongoDB\ Server\ 3.6\ log\ mongo. Cfg # Log output file path logappend=true # error log uses append mode journal=true # enable log file Enable quiet=true # by default to filter out some useless log information If you need to debug and use, please set the false port=27017 # port number to 27017 by default to install the MongoDB service: (cmd executes as an administrator) mongod-- config "C:\ Program Files\ MongoDB\ Server\ 3.6\ mongo.conf"-- install-- serviceName "MongoDB" start the service: cmd administrator startup > net start MongoDB II, basic syntax: MongoDB common data type String: the most commonly used stored data type. The string in MongoDB must be utf-8. Integer: used to store values. (32-bit / 64-bit) Boolean: stores Boolean values (true/false) Double: stores floating-point values. Min/Max keys: used to compare the lowest and highest values of the Bson element. Arrays: use this type of array / list / multiple values to store to a key Timestamp: timestamp. (convenient record files have been modified / added) Object: for embedded files. Null: stores null values. Symbol: the same for strings, but it is usually reserved for languages of specific symbol types. Date: stores the Unix time format of the current date / time. You can specify your own date and time / date and day of the year to create the object. ObjectID: the ID used to store the document. Binary data: stores binary data. Code: Javascript code that is stored in a document. Regular expression: used to store regular expression database add, delete, modify, create library / delete library / create collection / delete collection creation library: use db_name # use / create database (create database if it does not exist) db # check the currently selected database show dbs # shows which databases are currently available show tables/show collections # View Datasheet delete library: db.dropDatabase () # this will delete the selected database. If no database is selected, the Test database is deleted by default. Create collections: there is no concept of tables in mongodb. Collections (collections) are equivalent to tables in traditional databases. Syntax: db.createCollection (name,option) demo: > use test # enter the database switched to db test > db.createCollection ("mycollection") # create myCollection collection {"ok" 1} > show collections # check commands for creating collections mycollection system.indexes delete collections > use mydb # check available collections in database mydb switched to db mydb > show collections # check collection mycol mycollection > db.mycollection.drop () # Delete collection true drop () method returns true Successfully discarded Otherwise, return false > show collections # to check the collection list in the database again mycol system.indexes yiibaiMongoDB insert document (Insert () / save method) syntax: insert basic syntax such as: > db.COLLECTION_NAME.insert (document) db.mycol.insert ({title: 'MongoDB Overview') Description:'MongoDB id no sql database', by: 'tutorials yiibai', url:' http://www.yiibai.com', tags: ['mongodb','database','NoSQL'] Like: 100}) db.collection_name.find () # shows the inserted data is inserted into multiple documents: db.mycol.insert ([{}, {}]) db.score.insert ([{name: "Zhang San", subject: "Geography", score:88}) {name: "Li Si", subject: "Chinese", score:87}]) MongoDB update document (update () / save () method)

Updata () updates the existing document value / save () to replace the existing document value. The basic syntax of updata () is as follows: > db.COLLECTION_NAME.update (selection_criteria,updated_data) # mycol collection file: {"_ id": ObjectID (4685481851451we55), "title": "MongoDB overview"} {"_ id": ObjectID (4685481851451we56), "title": "NoSQL overview"} {"_ id": ObjectID (4685481851451we57), "title": "tutorials overview"} > db.mycol.update ({"title": "MongoDB overview"}) {$set: {"title": "tutorials overview"}) # Update title > db.mycol.update ({"title": "MongoDB overview"}, {$set: {"title": "tutorials overview"}}) {multi:true}) # to implement multiple updates followed by {multi:true} > db.mycol.find () # query inserted data save () method to replace all existing data that conforms to this ID document > db.collection_name.save ({id.ObjectID (), new_data}) db.mycol.save ({"_ id": ObjectID (5689745956adf56as7)) "title": "yiibai new topic", "by": "yiibai"}) > db.mycol.find () # query inserted data

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