One. MongoDB Insert Document Insert () method
To insert data into a MongoDB collection, you need to use the MongoDB Insert () or Save () method.
Grammar
The basic syntax for the Insert () command is as follows:
>db. Collection_name.insert (document)
Example
>db.mycol.insert({
_id: ObjectId(7df78ad8902c),
title: ‘MongoDB Overview‘,
description: ‘MongoDB is no sql database‘,
by: ‘tutorials point‘,
url: ‘http://www.yiibai.com‘,
tags: [‘mongodb‘, ‘database‘, ‘NoSQL‘],
likes: 100 })
Here MyCol is the name of the collection, as created in the previous tutorial. If the collection does not exist in the database, MongoDB creates the collection and then inserts it into the document. Insert the document, if we do not specify the _id parameter, then mongodb this document assigns a unique objectid.
_ID is a 12-byte hexadecimal number that is unique to each document in a collection. 12 bytes are divided as follows:
_id:objectid (4 bytes timestamp, 3 bytes machine ID, 2 bytes process ID, 3 bytes incrementer)
To insert multiple documents into a single query, you can pass a file of an array insert () command.
Example
>db.post.insert([
{
title: ‘MongoDB Overview‘,
description: ‘MongoDB is no sql database‘,
by: ‘tutorials point‘,
url: ‘http://www.yiibai.com‘,
tags: [‘mongodb‘, ‘database‘, ‘NoSQL‘],
likes: 100 },
{
title: ‘NoSQL Database‘,
description: ‘NoSQL database doesn‘t have tables‘,
by: ‘tutorials point‘,
url: ‘http://www.yiibai.com‘, tags: [‘mongodb‘, ‘database‘, ‘NoSQL‘],
likes: 20,
comments: [
{
user:‘user1‘,
message: ‘My first comment‘,
dateCreated: new Date(2013,11,10,2,35),
like: 0 }
]
}
])
To insert a file, you can also use Db.post.save (document). If _id is not specified in the document, then its save () method works the same as the insert () method. If _id is specified, it replaces the entire data file, which contains the _id specified save () method.
Two. MongoDB Query document Find () method
To query the collection data from MongoDB, you need to use the MongoDB find () method.
Grammar
The basic find () method syntax is as follows
>db. Collection_name.find ()
The Find () method displays all the files in an unstructured manner.
Pretty () method
The results are displayed in a formatted manner and can be used with the pretty () method.
Grammar:
>db.mycol.find (). Pretty ()
Example
>db.mycol.find().pretty()
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "tutorials yiibai",
"url": "http://www.yiibai.com",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
>
In addition to the find () method, there is a FindOne () method that returns a file.
RDBMS WHERE clause and MongoDB equivalent statement
To query the file based on some conditions, you can use the following actions
operation |
|
|
rdbms equivalent |
equality |
{< Key>:<value>} |
db.mycol.find ({"By": " Tutorials Yiibai "}). Pretty () |
where by = ' Tutorials Yiibai ' |
less Than |
{< key>:{$lt: <value>}} |
db.mycol.find ({"likes": {$lt:). Pretty () |
where likes < |
less Than Equals |
{ <key>:{$lte: <value>}} |
db.mycol.find ({"likes": {$lte:). Pretty () |
where likes <= |
greater Than |
{ <key>:{$gt: <value>}} |
db.mycol.find ({"likes": {$gt:). Pretty () |
where likes > |
greater Than Equals |
{<key>:{$gte: <value>}} |
Db.mycol.find ({"likes": {$gte:). Pretty () |
where likes >= |
Not Equals |
{<key>:{$ne: <value>}} |
Db.mycol.find ({"likes": {$ne:). Pretty () |
where likes! = 50 |
And in MongoDB usage syntax:
In the Find () method, if you detach ', ' through multiple keys, then MongoDB handles the and condition. The and basic syntax is as follows:
>db.mycol.find ({key1:value1, key2:value2}). Pretty ()
Example
The examples given below will show all tutorials, titled "MongoDB Overview"
>db.mycol.find({"by":"tutorials yiibai","title": "MongoDB Overview"}).pretty()
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "yiibai",
"url": "http://www.yiibai.com",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
>
For the example given above is equivalent to the WHERE clause ' where by= ' Yiibai ' and title= ' MongoDB Overview ', can pass any number of key-value pairs in the Find clause.
MongoDB in or syntax:
To query a file based on the or condition, you need to use the $or keyword. The OR basic syntax is as follows:
>db.mycol.find(
{
$or: [
{key1: value1}, {key2:value2}
]
}
).pretty()
Example
The examples given below will show all tutorials, written by ' Yiibai ' or titled "MongoDB Overview"
>db.mycol.find({$or:[{"by":"yiibai"},{"title": "MongoDB Overview"}]}).pretty()
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "yiibai",
"url": "http://www.yiibai.com",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
>
Use examples with and and OR
The example given below will show a file with the image greater than 100, with the title "MongoDB Overview" or "Yiibai". Equivalent to the SQL WHERE clause for ' where likes>10 and (by = ' Yiibai ' OR title = ' MongoDB Overview ') '
>db.mycol.find("likes": {$gt:10}, $or: [{"by": "yiibai"}, {"title": "MongoDB Overview"}] }).pretty()
{
"_id": ObjectId(7df78ad8902c),
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "yiibai",
"url": "http://www.yiibai.com",
"tags": ["mongodb", "database", "NoSQL"],
"likes": "100"
}
>
Three. MongoDB Update documentation
The MongoDB update () and Save () methods are used to update the collection of documents. The update () method updates the existing document value, replacing the existing document with the Save () method in the file.
MongoDB Update () method
The update () method updates the existing document values.
Grammar:
The basic syntax for the update () method is as follows
>db. Collection_name.update (Selectioin_criteria, Updated_data)
Example
Consider the following data MyCol collection.
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}
The following example will set the new title ' MongoDB Overview ' file and update its title to "New MongoDB Tutorial"
>db.mycol.update({‘title‘:‘MongoDB Overview‘},{$set:{‘title‘:‘New MongoDB Tutorial‘}})
>db.mycol.find()
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"New MongoDB Tutorial"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Yiibai Overview"}
>
MongoDB default will only update a single file to update multiple you need to set the parameter ' multi ' to True
>db.mycol.update ({' title ': ' MongoDB overview '},{$set: {' title ': ' New MongoDB Tutorial '}},{multi:true})
MongoDB Save () method
The Save () method replaces the existing document and the Save () method through the new document
Grammar
The basic syntax for the MongoDB Save () method is as follows:
>db. Collection_name.save ({_id:objectid (), new_data})
Example
The following example will replace the file with _id as ' 5983548781331adf45ec7 '
>db.mycol.save(
{
"_id" : ObjectId(5983548781331adf45ec7), "title":"Yiibai New Topic", "by":"Yiibai"
}
)
>db.mycol.find()
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"Yiibai New Topic", "by":"Yiibai"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Yiibai Overview"}
>
Four. MongoDB Delete Document Remove () method
The Remove () method of MongoDB is used to remove a document from the collection. The Remove () method accepts two parameters. The first one is to delete the criteria, the second is the JUSTONE flag:
-
Deletion criteria: (optional) Delete the standard, depending on the file will be deleted.
-
Justone: (optional) If set to TRUE or 1, then delete only one file.
Grammar:
The basic syntax of the Remove () method is as follows
>db. Collection_name.remove (Delletion_critteria)
Example
Consider the following data MyCol collection.
{ "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"}
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Yiibai Overview"}
The following example will delete all the files whose title is ' MongoDB Overview '
>db.mycol.remove({‘title‘:‘MongoDB Overview‘})
>db.mycol.find()
{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}
{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Yiibai Overview"}
>
Remove only one
If you have more than one record and you want to delete only the first record, set the Justone parameter in the Remove () method
>db. Collection_name.remove (deletion_criteria,1)
Delete all Files
If you do not specify a delete condition, then MongoDB will delete the entire file from the collection. This is equivalent to the SQL TRUNCATE command.
>db.mycol.remove()
>db.mycol.find()
>
MongoDB (eight) MongoDB document operations