Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Write operation of mongodb

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Use the insert data command:

> insert

It should be noted that if you insert a file (it is now understood that nosql type db data cannot be said to be a record, it is already a file as a unit, while in a traditional relational database, we say inserting a record), if there is no person _ id, then a unique id will be automatically generated, which is like the primary key in a relational database.

If you specify id yourself, then id must be unique, which is required by both relational and nosql types

The name of the data field cannot contain $and.

After knocking for a while, I found that if I were to write complex insert statements in shell, I would not kill me if I wrote a line like this in shell. I think it must support batch processing like bat or something, and see that its syntax is exactly the same as js, so I boldly guess that it uses javascript, so I use notepad++ to edit these insert commands and try to insert complex objects.

Oh, by the way, if the bios data does not exist, then use the

> use bios

Generate a database, and if you don't insert a piece of data here, mongoDB will automatically delete the database.

So we also need to insert a sum of arbitrary data for mongodb to maintain the database.

> db.bios.insert ({aVuza'})

Write an insert.js step and put it in the js folder of the installation directory. So enter this:

> mongolocalhost:27017/bios f:\ mongodb\ js\ insert.js

The effect is as shown in the picture

As you can see, this js has been successfully implemented.

To query this document, type the command:

> db.bios.find ({name: {first: 'John', last:' chen'}})

With js to write more statements, it will be much more convenient.

The Isert statement supports multiple bulk inserts like this:

Insert.js statement:

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. 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 ())

=

Mongo also has a save method, which also inserts data. How the same is the same, think carefully if it is the same, then why not unified, so think of the ORM schema, data save method, if the data to be saved already exists, then it knows to modify the data, if it does not exist, it is added. The same is true of save here.

If the save method has _ id, then, if the document of this _ id exists in the database, the original data will be modified. If not, it will be added, so the save method without _ id is the same as insert, but if the Insert method inserts the same Id, it will not be inserted.

Update operation, traditional database update operation, must be updated data already exists, otherwise you want to update your data records are not there, this is not nonsense, but mongodb can be like this, if the data to be updated does not exist, then insert the document data

Update syntax:

Db.collection.update (, {upsert: true})

If you are updating a specific one, the save method with _ id can also complete the update task.

The query can find changed data:

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

Summary

The above is the whole content of this article. I hope the content of this article has a certain reference and learning value for everyone's study or work. Thank you for your support. If you want to know more about it, please see the relevant links below.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report