How to use mongodb under handle Linux (1)

Time:2020-2-29

1. Create database

grammar

use DATABASE_NAME

Create a database of user? Data, and use the DB command to view

> use user_data
switched to db user_data
> db
user_data
>

View all databases using show DBS

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
test    0.000GB

As you can see, the newly created database user data is not in the list of databases. To display it, we need to insert some data into the user data database.

> db.user_data.insert ({name): "attention to official account, Ronaldinho technical notes, focus on research and knowledge sharing of development technology"}
WriteResult({ "nInserted" : 1 })
> show dbs
admin     0.000GB
config    0.000GB
local     0.000GB
user_data  0.000GB
test      0.000GB

The default database in mongodb is test. If you do not create a new database, the collection will be stored in the test database.

2. Delete database

grammar

db.dropDatabase()

The following operations will delete user data data

View all databases

> show dbs
admin     0.000GB
config    0.000GB
local     0.000GB
user_data  0.000GB
test      0.000GB

Switch to user data database

> use user_data
switched to db user_data

Using dB. Dropdatabase() to delete a database

> db.dropDatabase()
{ "dropped" : "user_data", "ok" : 1 }

See it has been deleted

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
test    0.000GB

3. Insert document type data

Use the insert() or save() method to insert a document into the collection. The syntax is as follows:

db.COLLECTION_NAME.insert(document)

The following documents can be stored in the col set of the user data database of mongodb:

Switch to user data database

> use user_data
switched to db user_data

DB. Col. insert ({Title: 'mongodb tutorial', 
    Description: 'mongodb is a NoSQL database',
    By: 'Xiaoluo technology notes - focus on research and knowledge sharing of development technology',
    url: 'http://www.yuwowugua.com',
    tags: ['mongodb', 'database', 'NoSQL'],
    likes: 100
})
> db.col.find()

Col in the above instance is our collection name. If the collection is not in the database, mongodb will automatically create the collection and insert the document.

To view an inserted document:

{
    "_id": ObjectId("5a69eb32ec3046ee8ae54dbb"),
    "Title": "mongodb tutorial",
    "Description": "mongodb is a NoSQL database",
    "By": "Xiaoluo technology notes - focusing on research and knowledge sharing of development technology",
    "url": "http://www.yuwowugua.com",
    "tags": ["mongodb", "database", "NoSQL"],
    "likes": 100
}

You can also define data as a variable, as follows:

Document = ({Title: 'mongodb tutorial', 
    Description: 'mongodb is a NoSQL database',
    By: 'Xiaoluo technology notes - focus on research and knowledge sharing of development technology',
    url: 'http://www.yuwowugua.com',
    tags: ['mongodb', 'database', 'NoSQL'],
    likes: 100
})

After execution, the results are as follows:

{
    "Title": "mongodb tutorial",
    "Description": "mongodb is a NoSQL database",
    "By": "Xiaoluo technology notes - focusing on research and knowledge sharing of development technology",
    "url" : "http://www.yuwowugua.com",
    "tags" : [
        "mongodb",
        "database",
        "NoSQL"
    ],
    "likes" : 100
}

To insert:

> db.col.insert(document)
WriteResult({ "nInserted" : 1 })

You can also insert documents using the db.col.save (document) command. If you do not specify the save() method of the ﹐ ID field, it is similar to the insert() method. If you specify the field, the data for that field is updated.

After version 3.2, the following syntax is available for inserting documents:

DB. Collection. Insertone(): inserts a document data into the specified collection.

DB. Collection. Insertmany(): inserts multiple document data into the specified collection.

Insert single data

>Var document = dB. Collection. Insertone ({"title": "Xiaoluo technology notes"})
> document
{
    "acknowledged" : true,
    "insertedId" : ObjectId("5a69f0dfec3046ee8ae54dbe")
}
> db.collection.find()
{"[u id": objectid ("5a69f0dfec3046ee8ae54dbe"), "title": "Xiao Luo's technical notes"}

Insert multiple data

> var res = db.collection.insertMany ([title]: "Ronaldinho technical note"}, {'description': "pay attention to the official account, Ronaldinho technical notes, focus on the development of technology research and knowledge sharing"}]
> res
{
    "acknowledged" : true,
    "insertedIds" : [
        ObjectId("5a69f188ec3046ee8ae54dbf"),
        ObjectId("5a69f188ec3046ee8ae54dc0")
    ]
}
> db.collection.find()
{"[u id": objectid ("5a69f0dfec3046ee8ae54dbe"), "title": "Xiao Luo's technical notes"}
{"_id": ObjectId ("5a69f188ec3046ee8ae54dc0"), "description": "pay attention to the official account, Ronaldinho's technical notes, focus on the research and knowledge sharing of development technology"}

4. Delete document

The remove() function is used to remove data from the collection.

Data updates can use the update() function. It is a good habit to execute the find() command before executing the remove() function to determine whether the execution conditions are correct.

The remove () method is out of date, and it is now officially recommended to use the deleteone () and deletemany () methods.

To delete all documents under the collection:

> db.col.deleteMany({})
{ "acknowledged" : true, "deletedCount" : 2 }
> db.col.find()

Delete all documents with title equal to “Xiao Luo’s technical notes”:

>DB. Collection. Deletemany ({Title: "Xiaoluo technology notes"})
{ "acknowledged" : true, "deletedCount" : 2 }
> 
> db.collection.find()
{"_id": ObjectId ("5a69f188ec3046ee8ae54dc0"), "description": "pay attention to the official account, Ronaldinho's technical notes, focus on the research and knowledge sharing of development technology"}

Delete a document with Weixin equal to “Xiaoluo technical notes”:

>DB. Col. insert ({Weixin: "Xiaoluo technology notes"})
WriteResult({ "nInserted" : 1 })
> db.col.find()
{"[u id": objectid ("5a69f5a0ec3046ee8ae54dc1"), "Weixin": "Xiao Luo's technical notes"}
>DB. Col. deleteone ({Weixin: "Xiaoluo technology notes"})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.col.find()

5. Query documents

The find () method is used to query documents.

The find () method displays all documents in an unstructured way.

The syntax format of query data is as follows:

> db.collection.find(query, projection)

Query: optional. Use the query operator to specify the query criteria

Projection: optionally, use the projection operator to specify the key returned. When querying, all the key values in the document are returned. You only need to omit this parameter (omitted by default).

The pretty () method displays all documents in a formatted way.

DB. Col. insert ({Title: 'mongodb tutorial', 
    Description: 'mongodb is a NoSQL database',
    By: 'Xiaoluo technology notes - focus on research and knowledge sharing of development technology',
    url: 'http://www.yuwowugua.com',
    tags: ['mongodb', 'database', 'NoSQL'],
    likes: 100
})

> db.col.find().pretty()
{
    "_id" : ObjectId("5a69f93598891b4abe9cc8cb"),
    "Title": "mongodb tutorial",
    "Description": "mongodb is a NoSQL database",
    "By": "Xiaoluo technology notes - focusing on research and knowledge sharing of development technology",
    "url" : "http://www.yuwowugua.com",
    "tags" : [
        "mongodb",
        "database",
        "NoSQL"
    ],
    "likes" : 100
}

Compare with RDBMS where statement
How to use mongodb under handle Linux (1)

5.1 and conditions

Mongodb’s find() method can pass in multiple keys. Each key is separated by commas, which is the and condition of regular SQL.

The syntax format is as follows:

> db.col.find({key1:value1, key2:value2}).pretty()

The following example uses the by and title keys to query data

>DB. Col. find ({"by": "Xiao Luo's technical notes - focusing on research and knowledge sharing of development technology", "title": "mongodb tutorial"}). Pretty ()
{
    "_id" : ObjectId("5a69f93598891b4abe9cc8cb"),
    "Title": "mongodb tutorial",
    "Description": "mongodb is a NoSQL database",
    "By": "Xiaoluo technology notes - focusing on research and knowledge sharing of development technology",
    "url" : "http://www.yuwowugua.com",
    "tags" : [
        "mongodb",
        "database",
        "NoSQL"
    ],
    "likes" : 100
}

In the above example, it is similar to where statement: where by = ‘Xiao Luo’s technical notes – focusing on research and knowledge sharing of development technology’ and title = ‘mongodb tutorial’

5.2 or conditions

The mongodb or conditional statement uses the keyword $or. The syntax format is as follows:

>db.col.find(
   {
      $or: [
         {key1: value1}, {key2:value2}
      ]
   }
).pretty

In the following example, we demonstrate the document with query key by value of “Xiaoluo technology notes – research and knowledge sharing focusing on development technology” or key Title Value of mongodb tutorial.

>DB. Col. find ({$or: [{"by": "Xiao Luo's technology notes - focusing on research and knowledge sharing of development technology"}, {"title": "mongodb tutorial"}]. Pretty ()
{
    "_id" : ObjectId("5a69f93598891b4abe9cc8cb"),
    "Title": "mongodb tutorial",
    "Description": "mongodb is a NoSQL database",
    "By": "Xiaoluo technology notes - focusing on research and knowledge sharing of development technology",
    "url" : "http://www.yuwowugua.com",
    "tags" : [
        "mongodb",
        "database",
        "NoSQL"
    ],
    "likes" : 100
}

5.3 combined use of and and or

The following example demonstrates the joint use of and and or, similar to the general SQL statement: ‘where likes > 50 and (by =’ Xiaoluo technical notes – focusing on research and knowledge sharing of development technology ‘or title =’ mongodb tutorial ‘)’

>DB. Col. find ({"likes": {$GT: 50}, $or: [{"by": "Xiao Luo's technical notes - research and knowledge sharing focusing on development technology"}, {"title": "mongodb tutorial"}]}). Pretty ()
{
    "_id" : ObjectId("5a69f93598891b4abe9cc8cb"),
    "Title": "mongodb tutorial",
    "Description": "mongodb is a NoSQL database",
    "By": "Xiaoluo technology notes - focusing on research and knowledge sharing of development technology",
    "url" : "http://www.yuwowugua.com",
    "tags" : [
        "mongodb",
        "database",
        "NoSQL"
    ],
    "likes" : 100
}

How to use mongodb under handle Linux (1)