MongoDB insert, view the document stepped pit remember

0 Introduction

All the dry technology hall

文章收录在我的 GitHub 仓库,欢迎Star/fork:

Java-Interview-Tutorial

https://github.com/Wasabi1234/Java-Interview-Tutorial

JSON data structure of the document and essentially the same.

All data stored in the collection are BSON format.

BSON is a similar binary storage format as JSON, Binary JSON is short.

MongoDB using insert () or save () method to insert a document to the collection, the following syntax:

db.COLLECTION_NAME.insert(document)

After selecting a database, use the collection to operate the document, insert the document syntax:

db.集合名称.insert(数据);

Insert the following test data:

db.comment.insert({content:"十次方课程",userid:"1011"})

A set of query syntax:

db.集合名称.find()
  • Query all documents spit collections, enter the following command:
db.comment.find()

Have found a document called _id field. This is equivalent to the primary key of our original relational database tables when you insert documentation does not specify this field, MongoDB is automatically created, its type is the type ObjectID. If we specify the field at the time of recording can also be inserted into the document, it can be a type ObjectID type, it can be any type of support for MongoDB.

Enter the following test statement:

db.comment.insert({_id:"1",content:"到底为啥出 错",userid:"1012",thumbup:2020}); db.comment.insert({_id:"2",content:"加班到半 夜",userid:"1013",thumbup:1023}); db.comment.insert({_id:"3",content:"手机流量超了咋 办",userid:"1013",thumbup:111}); db.comment.insert({_id:"4",content:"坚持就是胜 利",userid:"1014",thumbup:1223});

Under certain conditions to a query, the query such as userid record 1013 is added to the parameter, the parameter is json Format () as long as the find, as follows:

db.comment.find({userid:'1013'})

Only need to return the first qualifying data, we can use findOne command to achieve:

db.comment.findOne({userid:'1013'})

Returns the specified number of records, in the limit can be called to find methods return result, for example:

db.comment.find().limit(2)

Examples

The following documents can be stored in MongoDB col collection of runoob database:

>db.col.insert({title: 'MongoDB 教程', 
    description: 'MongoDB 是一个 Nosql 数据库',
    by: 'JavaEdge',
    url: 'http://www.baidu.com',
    tags: ['mongodb', 'database', 'NoSQL'],
    likes: 100
})

Col above example is our collection name, if the set is not in the database, MongoDB will automatically create the collection and insert the document.

View into the document:

> db.col.find()
{ "_id" : ObjectId("56064886ade2f21f36b03134"), "title" : "MongoDB 教程", "description" : "MongoDB 是一个 Nosql 数据库", "by" : "菜鸟教程", "url" : "http://www.runoob.com", "tags" : [ "mongodb", "database", "NoSQL" ], "likes" : 100 }
> 

We can also be defined as a data variable, as follows:

> document=({title: 'MongoDB 教程', 
    description: 'MongoDB 是一个 Nosql 数据库',
    by: 'JavaEdge',
    url: 'http://www.biadu.com',
    tags: ['mongodb', 'database', 'NoSQL'],
    likes: 100
});

After performing the display as follows:

{
        "title" : "MongoDB 教程",
        "description" : "MongoDB 是一个 Nosql 数据库",
        "by" : "JavaEdge",
        "url" : "http://www.biadu.com",
        "tags" : [
                "mongodb",
                "database",
                "NoSQL"
        ],
        "likes" : 100
}

Insert operation:

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

Into a document You can also use db.col.save (document) command. If no _id field save () method is similar to insert () method. If you specify _id field _id of the data is updated.

MongoDB MongoDB delete update the document collection

There are several version 3.2 syntax can be used to insert the document:

db.collection.insertOne (): Insert the document data to the designated one set
db.collection.insertMany (): Insert a plurality of document data to the designated collection

Insert single data

> var document = db.collection.insertOne({"a": 3})
> document
{
        "acknowledged" : true,
        "insertedId" : ObjectId("571a218011a82a1d94c02333")
}

Insert multiple data

> var res = db.collection.insertMany([{"b": 3}, {'c': 4}])
> res
{
        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("571a22a911a82a1d94c02337"),
                ObjectId("571a22a911a82a1d94c02338")
        ]
}

Inserting a plurality of data

1、先创建数组

2、将数据放在数组中

3、一次 insert 到集合中

var arr = [];

for(var i=1 ; i<=20000 ; i++){
    arr.push({num:i});
}

db.numbers.insert(arr);
Published 380 original articles · won praise 543 · views 330 000 +

Guess you like

Origin blog.csdn.net/qq_33589510/article/details/104703427