简单单个文档插入用insert方法:
1
2
|
> db.post.insert({
"bar"
:
"baz"
});
WriteResult({
"nInserted"
: 1 })
|
批量插入,用insert方法(参数要是一个文档数组):
1
2
3
4
5
6
7
8
9
10
11
12
|
> db.post.insert([{
"_id"
:0},{
"_id"
:1},{
"_id"
:2}]);
BulkWriteResult({
"writeErrors"
: [ ],
"writeConcernErrors"
: [ ],
"nInserted"
: 3,
"nUpserted"
: 0,
"nMatched"
: 0,
"nModified"
: 0,
"nRemoved"
: 0,
"upserted"
: [ ]
})
>
|
查看插入的结果:
1
2
3
4
5
6
|
> db.post.
find
();
{
"_id"
: ObjectId(
"54a51cfd7f46906f81b7adcd"
),
"bar"
:
"baz"
}
{
"_id"
: 0 }
{
"_id"
: 1 }
{
"_id"
: 2 }
>
|
附注:batchInsert方法在新版的mongoDB中已经不可用;
本文转自 bannerpei 51CTO博客,原文链接:http://blog.51cto.com/281816327/1598300,如需转载请自行联系原作者