mongodb 插入資料
> db.createCollection("obj");
{ "ok" : 1 }
{ "ok" : 1 }
插入一個文件
> db.obj.insert({
... name: "obj1",
... arr: [{elemName: "elem1", elemValue: 1}, {elemName: "elem2", elemValue: 2}],
... subObj: {
... subName: "subName1",
... subArr: ["subElem1", "subElem2", "subElem3"]
... }
... });
WriteResult({ "nInserted" : 1 })
... name: "obj1",
... arr: [{elemName: "elem1", elemValue: 1}, {elemName: "elem2", elemValue: 2}],
... subObj: {
... subName: "subName1",
... subArr: ["subElem1", "subElem2", "subElem3"]
... }
... });
WriteResult({ "nInserted" : 1 })
> db.obj.findOne()
{
"_id" : ObjectId("55029b16773fc1f962baf146"),
"name" : "obj1",
"arr" : [
{
"elemName" : "elem1",
"elemValue" : 1
},
{
"elemName" : "elem2",
"elemValue" : 2
}
],
"subObj" : {
"subName" : "subName1",
"subArr" : [
"subElem1",
"subElem2",
"subElem3"
]
}
}
{
"_id" : ObjectId("55029b16773fc1f962baf146"),
"name" : "obj1",
"arr" : [
{
"elemName" : "elem1",
"elemValue" : 1
},
{
"elemName" : "elem2",
"elemValue" : 2
}
],
"subObj" : {
"subName" : "subName1",
"subArr" : [
"subElem1",
"subElem2",
"subElem3"
]
}
}
以陣列形式,插入多行
var objs = [
{
name: "obj2",
arr: [{elemName: "elem1", elemValue: 1}, {elemName: "elem2", elemValue: 2}],
subObj: {
subName: "subName2",
subArr: ["subElem1", "subElem2", "subElem3"]
}
},
{
name: "obj3",
arr: [{elemName: "elem1", elemValue: 1}, {elemName: "elem2", elemValue: 2}],
subObj: {
subName: "subName3",
subArr: ["subElem1", "subElem2", "subElem3"]
}
}
];
{
name: "obj2",
arr: [{elemName: "elem1", elemValue: 1}, {elemName: "elem2", elemValue: 2}],
subObj: {
subName: "subName2",
subArr: ["subElem1", "subElem2", "subElem3"]
}
},
{
name: "obj3",
arr: [{elemName: "elem1", elemValue: 1}, {elemName: "elem2", elemValue: 2}],
subObj: {
subName: "subName3",
subArr: ["subElem1", "subElem2", "subElem3"]
}
}
];
> db.obj.insert(objs);
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
bulk方式批次插入
> var bulk = db.obj.initializeUnorderedBulkOp();
> bulk.insert(
... {
... name: "bulkObj1",
... arr: [{elemName: "bulkElemName1", elemValue: 100}, {elemName: "bulkElemName2", elemValue: 200}],
... subObj: {
... subName: "bulkSubObj1",
... subArr: ["bulkSubElem1", "bulkSubElem2", "bulkSubElem3"]
... }
... }
... );
> bulk.insert(
... {
... name: "bulkObj2",
... arr: [{elemName: "elem1", elemValue: 1}, {elemName: "elem2", elemValue: 2}],
... subObj: {
... subName: "bulkSubObj2",
... subArr: ["bulkSubElem1", "bulkSubElem2", "bulkSubElem3"]
... }
... }
... );
> bulk.insert(
... {
... name: "bulkObj1",
... arr: [{elemName: "bulkElemName1", elemValue: 100}, {elemName: "bulkElemName2", elemValue: 200}],
... subObj: {
... subName: "bulkSubObj1",
... subArr: ["bulkSubElem1", "bulkSubElem2", "bulkSubElem3"]
... }
... }
... );
> bulk.insert(
... {
... name: "bulkObj2",
... arr: [{elemName: "elem1", elemValue: 1}, {elemName: "elem2", elemValue: 2}],
... subObj: {
... subName: "bulkSubObj2",
... subArr: ["bulkSubElem1", "bulkSubElem2", "bulkSubElem3"]
... }
... }
... );
> bulk.execute();
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26239116/viewspace-1485423/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mongodb資料庫中插入資料MongoDB資料庫
- Python MongoDB 插入資料,已存在則不執行,不存在則插入PythonMongoDB
- mybatis插入資料、批量插入資料MyBatis
- MongoDB批量插入大資料文件的簡單方法一例MongoDB大資料
- MongoDB(7)- 文件插入操作MongoDB
- MongoDB不支援批量插入MongoDB
- MongoDB最簡單的入門教程之三 使用Java程式碼往MongoDB裡插入資料MongoDBJava
- oracle批量插入資料Oracle
- [MYSQL -19]插入資料MySql
- MyBatis 批量插入資料MyBatis
- 資料插入問題。
- Oracle 插入大量資料Oracle
- MongoDB資料庫MongoDB資料庫
- append插入不能多次未提交插入資料APP
- nodejs操作mongodb資料庫(mongodb)NodeJSMongoDB資料庫
- proc插入資料到資料庫資料庫
- oracle 大資料量資料插入Oracle大資料
- ColdFusion向資料庫插入資料例子資料庫
- mysql插入資料時如果有相同資料就不插入或者替換MySql
- MongoDB 集合的插入、更新、刪除操作MongoDB
- C++ Vector資料插入C++
- 每秒迴圈插入資料
- Mysql高效插入/更新資料MySql
- 12 可插入資料庫資料庫
- spark on yarn 資料插入mysqlSparkYarnMySql
- oracle插入隨機資料Oracle隨機
- MongoDB 資料庫操作MongoDB資料庫
- mongodb資料庫操作MongoDB資料庫
- .NET 資料庫大資料 方案(插入、更新、刪除、查詢 、插入或更新)資料庫大資料
- python如何將資料插入資料庫Python資料庫
- eazyexcel 讀取excel資料插入資料庫Excel資料庫
- MongoDB之資料查詢(資料排序)MongoDB排序
- Mysql在資料插入後立即獲取插入的IdMySql
- MySQL 資料庫表格建立、資料插入及獲取插入的 ID:Python 教程MySql資料庫Python
- 順序表有序插入資料
- mysql避免插入重複資料MySql
- 分庫分表插入資料
- 使用MySqlBulkLoader批量插入資料MySql