网站地图    收藏   

主页 > 后端 > MongoDB >

mongodb的写操作

来源:自学PHP网    时间:2019-08-01 18:22 作者:小飞侠 阅读:

[导读] mongodb的写操作...

使用插入数据命令:

哦,另外说一句,如果这个bios数据不存在,那么要先使用

db.bios.insert(
  [
   {
    _id: 3,
    name: { first: 'Grace', last: 'Hopper' },
    title: 'Rear Admiral',
    birth: new Date('Dec 09, 1906'),
    death: new Date('Jan 01, 1992'),
    contribs: [ 'UNIVAC', 'compiler', 'FLOW-MATIC', 'COBOL' ],
    awards: [
         {
          award: 'Computer Sciences Man of the Year',
          year: 1969,
          by: 'Data Processing Management Association'
         },
         {
          award: 'Distinguished Fellow',
          year: 1973,
          by: ' British Computer Society'
         },
         {
          award: 'W. W. McDowell Award',
          year: 1976,
          by: 'IEEE Computer Society'
         },
         {
          award: 'National Medal of Technology',
          year: 1991,
          by: 'United States'
         }
        ]
   },
   {
    _id: 4,
    name: { first: 'Kristen', last: 'Nygaard' },
    birth: new Date('Aug 27, 1926'),
    death: new Date('Aug 10, 2002'),
    contribs: [ 'OOP', 'Simula' ],
    awards: [
         {
          award: 'Rosing Prize',
          year: 1999,
          by: 'Norwegian Data Association'
         },
         {
          award: 'Turing Award',
          year: 2001,
          by: 'ACM'
         },
         {
          award: 'IEEE John von Neumann Medal',
          year: 2001,
          by: 'IEEE'
         }
        ]
   },
   {
    _id: 5,
    name: { first: 'Ole-Johan', last: 'Dahl' },
    birth: new Date('Oct 12, 1931'),
    death: new Date('Jun 29, 2002'),
    contribs: [ 'OOP', 'Simula' ],
    awards: [
         {
          award: 'Rosing Prize',
          year: 1999,
          by: 'Norwegian Data Association'
         },
         {
          award: 'Turing Award',
          year: 2001,
          by: 'ACM'
         },
         {
          award: 'IEEE John von Neumann Medal',
          year: 2001,
          by: 'IEEE'
         }
        ]
   }
  ]
);
db.bios.insert(
  {
   name: { first: 'John', last: 'McCarthy' },
   birth: new Date('Sep 04, 1927'),
   death: new Date('Dec 24, 2011'),
   contribs: [ 'Lisp', 'Artificial Intelligence', 'ALGOL' ],
   awards: [
        {
         award: 'Turing Award',
         year: 1971,
         by: 'ACM'
        },
        {
         award: 'Kyoto Prize',
         year: 1988,
         by: 'Inamori Foundation'
        },
        {
         award: 'National Medal of Science',
         year: 1990,
         by: 'National Science Foundation'
        }
       ]
  }
)
c = db.bios.find( { name: { first: 'John', last: 'McCarthy' } } );
while((c.hasNext())) printjson(c.next())

如果save方法带了_id那么,如果数据库中存在了此_id的文档,那么,会修改原有数据,如果没有,则新增,所以,不带_id的save方法和insert是一样的,但是Insert方法如果插入的是相同的Id,那么是不会插入的;

>c =db.bios.find( { name: { first: 'update', last: 'update' } } );
>while((c.hasNext()))printjson(c.next())

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对自学php网的支持。如果你想了解更多相关内容请查看下面相关链接

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论