MongoDB如何一次插入多条json数据--转

https://blog.csdn.net/qq_34579060/article/details/79408979

集合中的数据如下:

 

{ title: 'MongoDB Overview', description: 'MongoDB is no sql database', by_user: 'runoob.com', url: 'http://www.runoob.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 100 }, { title: 'NoSQL Overview', description: 'No sql database is very fast', by_user: 'runoob.com', url: 'http://www.runoob.com', tags: ['mongodb', 'database', 'NoSQL'], likes: 10 }, { title: 'Neo4j Overview', description: 'Neo4j is no sql database', by_user: 'Neo4j', url: 'http://www.neo4j.com', tags: ['neo4j', 'database', 'NoSQL'], likes: 750 },

如果使用如下操作,只会插入第一条数据

 

db.col.insert(
  {  
   "title": 'MongoDB Overview', 
   "description": 'MongoDB is no sql database',
   "by_user": 'runoob.com',
   "url": 'http://www.runoob.com',
   "tags": ['mongodb', 'database', 'NoSQL'],
   "likes": 100
  },
  {
   "title": 'MongoDB Overview', 
   "description": 'No sql database is very fast',
   "by_user": 'runoob.com',
   "url": 'http://www.runoob.com',
   "tags": ['mongodb', 'database', 'NoSQL'],
   "likes": 10
  },
  {
   "title": 'Neo4j Overview', 
   "description": 'Neo4j is no sql database',
   "by_user": 'runoob.com',
   "url": 'http://www.runoob.com',
   "tags": ['neo4j', 'database', 'NoSQL'],
   "likes": 750
  } 
)

如果一次性插入多条的话,应该如下:

 

db.col.insert(
	[
	  {
	   
	   "title": 'MongoDB Overview', 
	   "description": 'MongoDB is no sql database',
	   "by_user": 'runoob.com',
	   "url": 'http://www.runoob.com',
	   "tags": ['mongodb', 'database', 'NoSQL'],
	   "likes": 100
	},
	{
	  
	   "title": 'MongoDB Overview', 
	   "description": 'No sql database is very fast',
	   "by_user": 'runoob.com',
	   "url": 'http://www.runoob.com',
	   "tags": ['mongodb', 'database', 'NoSQL'],
	   "likes": 10
	},
	{
	  
	   "title": 'Neo4j Overview', 
	   "description": 'Neo4j is no sql database',
	   "by_user": 'runoob.com',
	   "url": 'http://www.runoob.com',
	   "tags": ['neo4j', 'database', 'NoSQL'],
	   "likes": 750
	}]
 )
    原文作者:noSql
    原文地址: https://www.cnblogs.com/davidwang456/articles/10955368.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞