MongoDB crud C

Source: Internet
Author: User
Tags mongo shell

Database operations
Command Operation
db View Current Database
show dbs See how many databases are on the current port
use db_name Switch database, automatically created without database
db.dropDatabase() Deleting a database

Other commands can be used db.help() or viewed on the website

Collection operations
Command Operation
db.collection_name.insert(doc) Automatically creates a collection when the document is inserted
db.collection_name.drop() Delete Collection
show collections Displays the collection in the current database

Other commands can be viewed db.mycoll.help() or viewed on the website

Document Insertion
Command Operation
db.createCollection(name, {size: ., capped: ., max: .} ) Create a specified database
db.collection.insertOne() Inserts a single document into the collection.
db.collection.insert() Inserts a single document or multiple documents into the collection.
db.collection.insertMany() Inserts multiple documents into a collection.
Create
  • Database

    • Create or switch databases

        $ mongo1 > Use companyswitched to DB Company2 >  Dbcompany3  
    • View current or all databases

        2 > Dbcompany3 > Show dbsadmin 0.000GBlocal 0.000GBtest 0.000gb4;  

      Note Because there is no data in the current database, you cannot see

    • Delete database

        4 > Dbcompany5 > Db.dropdatabase () {" OK ": 1}6 > Dbcompany7;  

      After deleting the current database, it is strange that the current number The library is still company

  • Collection

    • Inserting documents and automatically creating collections

      11 > db.unicorns.insert({...      name: 'Aurora',...      gender: 'f',...      weight: 450... })WriteResult({ "nInserted" : 1 })

      After inserting the document, the data is available, so the database is viewed

      12 > show dbsadmin    0.000GBcompany  0.000GBlocal    0.000GBtest     0.000GB
    • Displays the collection in the current database

      13 > show collectionsunicorns
    • List Display collection name

      14 > db.getCollectionNames()[ "unicorns" ]
    • Delete Collection

      15 > db.unicorns.drop()true16 > show collections17 > db.getCollectionNames()[ ]18 >
  • Document

    • Insert a piece of data

      18 > db.unicorns.insert({...      name    : 'Aurora',...      gender  : 'f',...      weight  : 450... })WriteResult({ "nInserted" : 1 })
    • Inserting more than one data

      Insert too much data, use JS document

      DB.Unicorns.Insertmany([{        name: ' horny ',        DOB: New Date(1992,2, -,7, -),        loves:[' Carrot ',' Papaya '],        Weight:  -,        Gender: ' m ',        Vampires:  the},    {        name: ' Aurora ',        DOB: New Date(1991, 0,  -,  -, 0),        loves:[' Carrot ', ' Grape '],        Weight:  the,        Gender: ' F ',        Vampires:  +},    {        name: ' Unicrom ',        DOB: New Date(1973, 1, 9,  A, Ten),        loves:[' Energon ', ' Redbull '],        Weight: 984,        Gender: ' m ',        Vampires: 182},    {        name: ' Roooooodles ',        DOB: New Date(1979, 7,  -,  -,  -),        loves:[' Apple '],        Weight: 575,        Gender: ' m ',        Vampires:  About},    {        name: ' Solnara ',        DOB: New Date(1985, 6, 4, 2, 1),        loves:[' Apple ', ' Carrot ',        ' Chocolate '],        Weight:550,        Gender:' F ',        Vampires: the},    {        name:' Ayna ',        DOB: New Date(1998, 2, 7, 8,  -),        loves:[' Strawberry ', ' Lemon '],        Weight: 733,        Gender: ' F ',        Vampires:  +},    {        name:' Kenny ',        DOB: New Date(1997, 6, 1, Ten,  the),        loves:[' Grape ', ' Lemon '],        Weight: 690,        Gender: ' m ',        Vampires:  the},    {        name: ' Raleigh ',        DOB: New Date(2005, 4, 3, 0,  $),        loves:[' Apple ', ' sugar '],        Weight: 421,        Gender: ' m ',        Vampires: 2},    {        name: ' Leia ',        DOB: New Date(2001, 9, 8,  -,  -),        loves:[' Apple ', ' Watermelon '],        Weight: 601,        Gender: ' F ',        Vampires:  -},    {        name: ' Pilot ',        DOB: New Date(1997, 2, 1, 5, 3),        loves:[' Apple ', ' Watermelon '],        Weight: 650,        Gender: ' m ',        Vampires:  Wu},    {        name: ' Nimue ',        DOB: New Date(1999,  One,  -,  -,  the),        loves:[' Grape ', ' Carrot '],        Weight: 540,        Gender: ' F '},    {        name: ' Dunx ',        DOB: New Date(1976, 6,  -,  -,  -),        loves:[' Grape ', ' Watermelon '],        Weight: 704,        Gender: ' m ',        Vampires: 165}])

      Mongo Shell uses load ("Insertm.js")

      19 > load("insertM.js")true20 > db.unicorns.find(){ ..., "name" : "Aurora", "gender" : "f", "weight" : 450 }{ ..., "name" : "Horny", "dob" : ISODate("1992-03-12T23:47:00Z"), ...}{ ..., "name" : "Aurora", "dob" : ISODate("1991-01-24T05:00:00Z"), ... }......21 > db.unicorns.count()13

      You can see that 13 data has been inserted

    • Insert multiple or one piece of data using insert

      • When inserting a bar, the syntax is the same as Insertone
      • Syntax is the same as insertmany when inserting multiple bars
    • Looping multiple data inserts

      22 > for(i = 3; i < 100;  i ++)db.set2test.insert({x:i})WriteResult({ "nInserted" : 1 })23 > db.set2test.count()97

      You can see that a loop is used to insert and create the collection set2test , with 97 data in the collection after the insert is complete

      ?

MongoDB crud C

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.