MongoDB Basic Usage

Source: Internet
Author: User
Tags mongodb tutorial

MongoDB is an open-source database system based on distributed file storage. The data is stored as a document and data structures are composed of key-value (key=>value) pairs.

A MongoDB document is similar to a JSON object. Field values can contain other documents, arrays, and array of documents.

Start MongoDB Service

CMD into the F:\MongoDB\3.4\bin directory, enter the start command:mongod--dbpath F:\MongoDB\data\db, 27017 words indicating that the service started successfully

Possible errors encountered: shutting down withcode:100

1. Admin start: win+x, then a key, start cmd

2. Set up the MongoDB service as a Windows service:

Mongod--dbpath= "F:\MongoDB\db"--logpath= "F:\MongoDB\Log\mongod.log"--logappend--directoryperdb--servicename " MongoDB "--servicedisplayname" MongoDB "--install

3. Delete Files Mongod.lock and Storage.bson

or Computer Management-services-start

Basic Operation Syntax
1 #Create a database, to display the database, you need to insert at least one file2 Use TestDB3 #Create collection, options (optional) Specify the memory size and index options, in MongoDB, you do not need to create a collection. When inserting some files MongoDB automatically creates the collection4Db.createcollection ("Col", Options)5 #Deleting a database6 db.dropdatabase ()7 #Delete Collection8 Db.collection.drop ()9 Ten #Inserting Data OneDb.testDB.insert ({"name":"Beginner's Tutorial"}) ADb.col.insert ({title:'MongoDB Tutorials',  -Description'MongoDB is a Nosql database', -By'Beginner's Tutorial', theUrl:'http://www.runoob.com', -Tags: ['MongoDB','Database','NoSQL'], -likes:100 - }) + #inserting a document using the Insert () or Save () method to insert a document into the collection -Document= ({title:'MongoDB Tutorials',  +Description'MongoDB is a Nosql database', ABy'Beginner's Tutorial', atUrl:'http://www.runoob.com', -Tags: ['MongoDB','Database','NoSQL'], -likes:100 - }); - Db.col.insert (document) -Db.col.save (document)#If you do not specify the _id field, the Save () method is similar to the Insert () method. If the _id field is specified, the data for the _id is updated.  in  - #Querying documents to Db.mycol.find (). Pretty () + Db.mycol.find ({key1:value1, key2:value2}). Pretty () -Db.mycol.find ({$or: [{key1:value1}, {key2:value2}]}). Pretty () the  *Db.mycol.find ({" by":"Tutorials Yiibai","title":"MongoDB Overview"}). Pretty () $Db.mycol.find ({$or:[{" by":"Yiibai"},{"title":"MongoDB Overview"}]}). Pretty ()Panax NotoginsengDb.mycol.find ("likes": {$gt: 10}, $or: [{" by":"Yiibai"}, {"title":"MongoDB Overview"}]}). Pretty () -  the #update the document by default will only update a single file to update multiple you need to set the parameter ' multi ' to true + db. Collection_name.update (Selectioin_criteria, Updated_data) A  theDb.mycol.update ({'title':'MongoDB Overview'},{$set: {'title':'New MongoDB Tutorial'}}) +Db.mycol.update ({'title':'MongoDB Overview'},{$set: {'title':'New MongoDB Tutorial'}},{multi:true}) -  $ db. Collection_name.save ({_id:objectid (), new_data}) $Db.mycol.save ({"_id": ObjectId (5983548781331ADF45EC7),"title":"Yiibai New Topic"," by":"Yiibai"}) -  - #Delete document, Justone optional set to TRUE or 1, delete only one file the db. Collection_name.remove (Delletion_critteria,justone) - WuyiDb.mycol.remove ({'title':'MongoDB Overview'}) theDb.mycol.remove ({'title':'MongoDB Overview'},1) -  Wu #query projection, when the Find () method is executed, it displays all fields of a document. To limit this, you need to set the field List value 1 or 0. 1 is used to display fields and 0 is used to hide fields -Db. Collection_name.find ({},{key:1}) AboutDb.mycol.find ({},{"title": 1, _id:0}) ${"title":"MongoDB Overview"} -{"title":"NoSQL Overview"} -{"title":"Yiibai Overview"} -  A #sort, 1 for ascending order, and 1 for descending, default ascending +Db. Collection_name.find (). Sort ({key:1}) theDb.mycol.find (). Sort ({"title":-1}) - #Index $Db. Collection_name.ensureindex ({key:1}) theDb.mycol.ensureIndex ({"title": 1})

Reference: Beginner's Tutorial

Mongdb Importing and exporting files

1. Import

Example:mongoimport-d douban-c film--type csv--headerline --file C:\Users\HP\Desktop\codes\DATA\ratings.csv

Grammar Essentials

-D: Database name

-c:collection Name

--type: Imported format default JSON

-F: Imported field names

--headerline: If the imported format is CSV, you can use the header of the first row as the imported field

--file: The file to import

2. Export

Example:mongoexport-d douban-c info-o C:\Users\HP\Desktop\codes\DATA\douban.csv--type csv-f "_id,rate,tag,title"

Syntax Essentials:

-D: Database name

-c:collection Name

-O: The file name of the output

--type: The format of the output, the default is JSON

-F: The Output field, if-type is CSV, you need to add-f "field name"

May encounter error:syntaxerror:missing; Before statement @ (Shell): 1:15

Workaround : Open a cmd write, not in the run MONGO cmd inside, is not a very magical solution

MongoDB Basic Usage

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.