data type
- The table below for MongoDB Several data types commonly used in :
- Object ID: file ID
- String: character string , The most commonly used , Must be effective UTF-8
- Boolean: Store a Boolean value ,true or false
- Integer: The whole number can be 32 Bit or 64 position , It depends on the server
- Double: Store floating point values
- Arrays: Array or list , Multiple values are stored in a key
- Object: For embedded documents , That is, a value is a document
- Null: Storage Null value
- Timestamp: Time stamp
- Date: Storing the current date or time UNIX Time format
object id
- Every document has an attribute , by _id, Make sure every document is unique
- You can set it yourself _id Inserted into the document
- If not provided , that MongoDB Provides a unique... For each document _id, The type is objectID
- objectID It's a 12 Hexadecimal number of bytes
- front 4 Bytes are the current timestamp
- Next 3 Byte machine ID
- Next 2 In bytes MongoDB The service process of id
- Last 3 A byte is a simple incremental value
Insert
- grammar
db. Collection name .insert(document)
- When inserting a document , If you don't specify _id Parameters ,MongoDB The document is assigned a unique ObjectId
- example 1
db.stu.insert({name:'gj',gender:1})
- example 2
s1={_id:'20160101',name:'hr'}
s1.gender=0
db.stu.insert(s1)
Simple query
- grammar
db. Collection name .find()
to update
- grammar
db. Collection name .update(
<query>,
<update>,
{multi: <boolean>}
)
- Parameters query: Query criteria , similar sql sentence update in where part
- Parameters update: Update Operators , similar sql sentence update in set part
- Parameters multi: Optional , The default is false, Only the first record found is updated , The value is true It means to update all the documents that meet the conditions
- example 3: Full document update
db.stu.update({name:'hr'},{name:'mnc'})
- example 4: Specify property update , By the operator $set
db.stu.insert({name:'hr',gender:0})
db.stu.update({name:'hr'},{$set:{name:'hys'}})
- example 5: Modify multiple matched data
db.stu.update({},{$set:{gender:0}},{multi:true})
preservation
- grammar
db. Collection name .save(document)
If the document _id Modify if it already exists , If the document _id Add if not
example 6
db.stu.save({_id:'20160102','name':'yk',gender:1})
- example 7
db.stu.save({_id:'20160102','name':'wyk'})
Delete
- grammar
db. Collection name .remove(
<query>,
{
justOne: <boolean>
}
)
- Parameters query: Optional , Conditions for deleted documents
- Parameters justOne: Optional , If it is set to true or 1, Only one , Default false, Means to delete multiple
- example 8: Delete only the first matching
db.stu.remove({gender:0},{justOne:true})
- example 9: Delete all
db.stu.remove({})
About size An example of
- example 10
- Create set
db.createCollection('sub',{capped:true,size:10})
- Insert the first database query
db.sub.insert({title:'linux',count:10})
db.sub.find()
- Insert the second database query
db.sub.insert({title:'web',count:15})
db.sub.find()
- Insert the third database query
db.sub.insert({title:'sql',count:8})
db.sub.find()
- Insert the fourth database query
db.sub.insert({title:'django',count:12})
db.sub.find()
- Insert the fifth database query
db.sub.insert({title:'python',count:14})
db.sub.find()
Data query
The basic query
- Method find(): Inquire about
db. Collection name .find({ Condition document })
- Method findOne(): Inquire about , Just return the first one
db. Collection name .findOne({ Condition document })
- Method pretty(): Format the results
db. Collection name .find({ Condition document }).pretty()
Comparison operator
- be equal to , Default is equal to judgment , There are no operators
- Less than $lt
- Less than or equal to $lte
- Greater than $gt
- Greater than or equal to $gte
- It's not equal to $ne
- example 1: The query name is equal to 'gj' Of the students
db.stu.find({name:'gj'})
- example 2: The query age is greater than or equal to 18 Of the students
db.stu.find({age:{$gte:18}})
Logical operators
- When querying, there can be multiple conditions , Multiple conditions need to be connected by logical operators
- Logic and : The default is the relationship between logic and
- example 3: The query age is greater than or equal to 18, And the gender is 1 Of the students
db.stu.find({age:{$gte:18},gender:1})
- Logic or : Use $or
- example 4: Query age is greater than 18, Or gender is 0 Of the students
db.stu.find({$or:[{age:{$gt:18}},{gender:1}]})
- and and or Use it together
- example 5: Query age is greater than 18 Or gender is 0 Of the students , And the name of the student is gj
db.stu.find({$or:[{age:{$gte:18}},{gender:1}],name:'gj'})
Range operator
- Use "$in","$nin" Judge whether it is in a certain range
- example 6: Query age is 18、28 Of the students
db.stu.find({age:{$in:[18,28]}})
regular expression
- Use // or $regex Write regular expressions
- example 7: Look up the student surnamed Huang
db.stu.find({name:/^ yellow /})
db.stu.find({name:{$regex:'^ yellow '}}})
Custom query
- Use $where Then write a function , Return data that meets the criteria
- example 7: Query age is greater than 30 Of the students
db.stu.find({$where:function(){return this.age>20}})
mongodb More related articles about the addition, deletion and modification of data
- mongodb Installation, addition, deletion and modification of
mongodb Is a distributed file storage database , Notice these two words , Distributed and file storage .mongodb Supports replication and fragmentation , The size of space can be used reasonably , It can also achieve the purpose of disaster recovery . In addition, file storage is also a feature , Abandon the traditional concept of watch ...
- fourteen :SpringBoot- To configure MongoDB database , Add, delete, modify and query logic
SpringBoot- To configure MongoDB database , Add, delete, modify and query logic 1.MongoDB database 1.1 MongoDB brief introduction 1.2 MongoDB characteristic 2.SpringBoot Integrate MongoDB 2.1 ...
- Mybatis Frameworks are based on annotations , The actual data is added, deleted, modified and checked
To write Mybatis Code , And spring Dissimilarity , No need to import plug-ins , Just import the shelf bag : stay lib Next Import mybatis Rack bag :mybatis-3.1.1.jarmysql Drive frame package :mysql-connecto ...
- dbutils The method of adding, deleting, modifying and querying data in , Common methods of reflection , How to write absolute path ( Miscellany )
jsp The three instructions of are :page,include,taglib... Build a jsp file , Build an absolute path , When using , other jsp The file can be imported The import method :<%@ include file="/c ...
- MVC Pattern : Realize the function of adding, deleting, modifying and querying the data in the database
*. Database connection pool c3p0, Connect mysql database : *.Jquery Use , Jump out of box on delete , Determine if you want to delete : *. Use EL and JSTL, Simplify in jsp Page java Language 1. Connect to database (1) Import connection data ...
- Hibernate3 review -5- Brief introduction Hibernate session Add, delete, modify and search the data
5. Hibernate Add, delete, modify and search the data 5.1Hibernate Load data Two kinds of :get().load() One . Session.get(Class arg0, Serializable arg1) Fang ...
- Mybatis Learning summary ( Two )— Use the interface to add, delete, modify and query data
In this article , Let's use the interface to add, delete, modify and query user data . The completed project structure is shown in the figure below : ad locum ,person The class of an entity represents . In this class , Describes the relevant information , Include id.name.age.id_num ...
- Data addition, deletion, modification and query ( Three layers )<!-- To be added -->
Data operation is bound to reduce the number of data addition, deletion, modification and query , The code generated by the code generator is not so satisfactory ! Easy to use in the future , This is mainly about “ Data access layer (Dal)” Now that we're talking about three-tier architecture : It is necessary to introduce the three-tier content in detail here ( To be added ) notes : ...
- vue Realize the addition, deletion, modification and query of table data
In some background pages of the Administrator , In the data list in the personal Center , There will be operations to add, delete, modify and query these data . For example, in the user list of the administrator background , We can enter information about new users , You can also modify the existing user information . stay vue in , We should focus more on ...
Random recommendation
- Imitation 【Emmet】 turn 【HTML】 function
One . Define regular expressions : var whitespace = "[\\x20\\t\\r\\n\\f]*", nvarcharEncoding = whitespace + " ...
- To configure apache Virtual domain name
Apache Modification of configuration file . ----> Apache-----> httpd.conf, open httpd.conf file . 1) find :#LoadModule rewrite_modu ...
- utilize win The service is enabled regularly for the network card / Ban
Last week, ,Boss Tell me , His son gets up at night and plays in bed with his notebook CF, Can router solve this problem , I saw it. It was TPLINK Ordinary home wireless router , Disable... Is not supported CF Client online games , You can configure ports to shield things , But it's on again during the day , Think ...
- Type conversion helper class TypeCaseHelper
package org.sakaiproject.util; import java.math.BigDecimal; import java.sql.Date; import java.sql.Ti ...
- bootstrap-datepicker Use
Reprinted from :http://michael-roshen.iteye.com/blog/1779541 Display... In a normal web page datepicker Relatively simple , take bootstrap-datepicker-zh_CN ...
- 10 Great classic CSS3 Menu application appreciation
A lot of times , Our web menu needs to be personalized , So as to adapt to the user visual operation experience of various industries . This article will lead you to enjoy 10 It's a very classic CSS3 Menu application , The menu involves the animation menu .Tab menu . Bread crumbs, menu, etc . 1.CSS3 Ribbon shape 3 ...
- ADC Driver or differential amplifier design guide
As an application engineer , We often come across all kinds of high speed ADC with differential input (ADC) It's a driving problem . in fact , Choose the right one ADC Drives and configurations are challenging . For robustness ADC Circuit design is somewhat easier , We've compiled a set of general “ Roadblock ” And solve ...
- 【 interview 】 Two aspects of mushroom Street product operation & result
2015-08-25 I got a call from Hangzhou at about 3:30 this afternoon , It's an interviewer from mushroom street , The interviewer said at the beginning that let's make a simple 15 Let's have a minute interview . First , Let me do a self introduction related to the product experience , I talked about my product practice and two product competitions ...
- AKOJ -- 1529 -- Look for the maximum number
1529: Look for the maximum number Time Limit: 1 Sec Memory Limit: 128 MB Submit: 107 Solved: 53 Last question SubmitStatus Label score edit Title letter ...
- [ Weave a message framework ][JAVA The core technology ] Dynamic proxy applications 11- Horizontal expansion to achieve
Because of the example , The remote service address is configured in properties file , adopt QMConfig Class loading , The best way is to , On shared memory , Just maintain one copy of the data , If placed in redis On /** Service address <servic ...