MongoDB CRUD之C
时间:2021-07-01 10:21:17
帮助过:5人阅读
.
unicorns.
insertMany([
{
name: 'Horny',
dob: new Date(
1992,2,13,7,47)
,
loves: [
'carrot','papaya']
,
weight: 600,
gender: 'm',
vampires: 63},
{
name: 'Aurora',
dob: new Date(
1991, 0, 24, 13, 0)
,
loves: [
'carrot', 'grape']
,
weight: 450,
gender: 'f',
vampires: 43},
{
name: 'Unicrom',
dob: new Date(
1973, 1, 9, 22, 10)
,
loves: [
'energon', 'redbull']
,
weight: 984,
gender: 'm',
vampires: 182},
{
name: 'Roooooodles',
dob: new Date(
1979, 7, 18, 18, 44)
,
loves: [
'apple']
,
weight: 575,
gender: 'm',
vampires: 99},
{
name: 'Solnara',
dob: new Date(
1985, 6, 4, 2, 1)
,
loves:[
'apple', 'carrot',
'chocolate']
,
weight:550,
gender:'f',
vampires:80},
{
name:'Ayna',
dob: new Date(
1998, 2, 7, 8, 30)
,
loves: [
'strawberry', 'lemon']
,
weight: 733,
gender: 'f',
vampires: 40},
{
name:'Kenny',
dob: new Date(
1997, 6, 1, 10, 42)
,
loves: [
'grape', 'lemon']
,
weight: 690,
gender: 'm',
vampires: 39},
{
name: 'Raleigh',
dob: new Date(
2005, 4, 3, 0, 57)
,
loves: [
'apple', 'sugar']
,
weight: 421,
gender: 'm',
vampires: 2},
{
name: 'Leia',
dob: new Date(
2001, 9, 8, 14, 53)
,
loves: [
'apple', 'watermelon']
,
weight: 601,
gender: 'f',
vampires: 33},
{
name: 'Pilot',
dob: new Date(
1997, 2, 1, 5, 3)
,
loves: [
'apple', 'watermelon']
,
weight: 650,
gender: 'm',
vampires: 54},
{
name: 'Nimue',
dob: new Date(
1999, 11, 20, 16, 15)
,
loves: [
'grape', 'carrot']
,
weight: 540,
gender: 'f'},
{
name: 'Dunx',
dob: new Date(
1976, 6, 18, 18, 18)
,
loves: [
'grape', 'watermelon']
,
weight: 704,
gender: 'm',
vampires: 165}
])
Mongo shell使用load("insertM.js")
19 > load("insertM.js")
true
20 > db.unicorns.find()
{ ..., "name" : "Aurora", "gender" : "f", "weight" : 450 }
{ ..., "name" : "Horny", "dob" : ISODate("1992-03-12T23:47:00Z"), ...}
{ ..., "name" : "Aurora", "dob" : ISODate("1991-01-24T05:00:00Z"), ... }
......
21 > db.unicorns.count()
13
可以看到插入了13条数据
插入多条或者一条数据使用insert
- 插入一条时语法与insertOne一样
- 插入多条时语法与insertMany一样
循环插入多条数据
22 > for(i = 3; i < 100; i ++)db.set2test.insert({x:i})
WriteResult({ "nInserted" : 1 })
23 > db.set2test.count()
97
可以看到使用了循环插入并创建集合 set2test
,插入完成后集合中有了97条数据
?
MongoDB CRUD之C
标签:插入数据 循环 pos 多少 write oca count log imu