A brief introduction to NoSQL
MongoDB is a nosql. What is nosql,nosql (NoSQL = not-only sql), meaning "not just sql".
NoSQL, refers to a non-relational database. NoSQL, sometimes referred to as the abbreviation of not-only SQL, is a generic term for a database management system that differs from a traditional relational database.
NoSQL is used for storage of hyper-scale data. (for example, Google or Facebook collects trillions of bits of data for their users every day). These types of data stores do not require a fixed pattern and can be scaled horizontally without extra action.
MongoDB Introduction MongoDB provides a document-oriented storage that is simple and easy to operate. You can set the index of any property in the MongoDB record (for example: Firstname= "Sameer", address= "8 Gandhi Road") for faster sorting. You can create data mirroring either locally or on the network, which makes MongoDB more extensible. MongoDb uses the update () command to implement a replacement of the completed document (data) or some specified data fields. The map/reduce in MongoDB is primarily used for batch processing and aggregation of data. Map and reduce. The map function calls emit (Key,value) to traverse all records in the collection, passing key and value to the reduce function for processing. The map function and the reduce function are written in JavaScript and can be executed with the Db.runcommand or MapReduce command. Gridfs is a built-in feature in MongoDB that can be used to store a large number of small files. MongoDB allows you to execute scripts on the server, write a function in JavaScript, execute directly on the server, or store the definition of the function on the server, next time you call it directly. MongoDB supports a variety of programming languages: ruby,python,java,c++,php,c# and many more languages. MongoDB installation is simple. Spring integration using MongoDB
I use MongoDB myself, which is used with the MongoDB package in spring. As long as the jar package is, the following is the MAVEN reference package
<dependency>
<groupId>org.mongodb</groupId>
<artifactid>mongo-java-driver</ artifactid>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactid>spring-data-mongodb</ artifactid>
<version>1.6.2.RELEASE</version>
</dependency>
Configure a MongoDB data sourceThe data connection address. Save with a properties.
mongo.dburl=172.16.40.18:27017
mongo.dbname=qingxing
mongo.connectionsperhost=100
mongo.threadsallowedtoblockforconnectionmultiplier=4
mongo.maxwaittime=1500
mongo.sockettimeout=1500
mongo.connecttimeout=1000
mongo.autoconnectretry=true
mongo.socketkeepalive=true
Mongo.slaveok=true
To configure a data source in a spring configuration file
<!--****************************** MongoDB begin **********************************-<!--settings for some connection properties--&G
T <mongo:mongo id= "MONGO" replica-set= "${mongo.dburl}" > <mongo:options connections-per-host= "${mongo.connec Tionsperhost} "threads-allowed-to-block-for-connection-multiplier=" ${mongo.threadsallowedtoblockforconnectionm
Ultiplier} "connect-timeout=" ${mongo.connecttimeout} "Max-wait-time=" ${mongo.maxwaittime} "
auto-connect-retry= "${mongo.autoconnectretry}" socket-keep-alive= "${mongo.socketkeepalive}"
socket-timeout= "${mongo.sockettimeout}" slave-ok= "${mongo.slaveok}" write-number= "1" write-timeout= "0" write-fsync= "true"/> </mongo:mongo> <!--MONGO Factory, through which to obtain MONGO instances, DBN AME is the database name of MongoDB, no words are automatically created--<mongo:db-factory dbname= "${mongo.dbname}" mongo-ref= "Mongo"/> <bean I D= "Mappingcontext"class=" Org.springframework.data.mongodb.core.mapping.MongoMappingContext "/> <!--remove the _class attribute in the collection-->
; <bean id= "Defaultmongotypemapper" class= "Org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper "> <constructor-arg name=" typekey "> <null/> </constructor-arg> </ bean> <bean id= "Mappingmongoconverter" class= "Org.springframework.data.mongodb.core.convert.MappingMongo Converter "> <constructor-arg name=" mongodbfactory "ref=" mongodbfactory "/> <constructor-arg na
Me= "Mappingcontext" ref= "Mappingcontext"/> <property name= "typemapper" ref= "Defaultmongotypemapper"/> </bean> <!--MongoDB's main operating objects, all the additions and deletions to MongoDB are done through it-<bean id= "mongotemplate" class= "org.sp Ringframework.data.mongodb.core.MongoTemplate "> <constructor-arg name=" mongodbfactory "ref=" Mongodbfactory " /> <coNstructor-arg name= "Mongoconverter" ref= "Mappingmongoconverter"/> </bean> <!--map Converter, scan back-package The files under the directory, according to the annotations, map them as a collection of MongoDB---<mongo:mapping-converter base-package= "Com.qx.mongodb.doc"/ > <!--the repository directory of MongoDB beans, automatically scans the interface that extends the Mongorepository interface for injection--<mongo:repositories base-package= "com.q
X.mongodb.dao "/> <!--***************************** MongoDB over **********************************--
The operation of the object. Establishes mapping relationship correspondence.
/**
* Coach version of The Voice broadcast
*
* @author Luoyang * */
@Document (collection = "Mg_voice") //Deposit table name Public
class Mgvoice {
@Id //Flag primary key
private Long CID;
/**
* M male F female voice */
private String voicetype;
/**
* Speed */
private Integer voicespeed;
Private list<mgvoicelibrary> Voicelibrarys;
Private list<mgvoiceplan> Voiceplans;
Public Long Getcid () {
return cid;
}
....
Inline Object Mgvoicelibrary
/** *
trainer version of the Voice library
* * @author Luoyang * * */public
class Mgvoicelibrary {
private String lid;
/**
* 2 subjects 23 Subjects three 4 starting lights
*/
private Integer type;
Private String title;
Private String content;
/**
* 0 is not 1 is
*/
private Integer istemp;
Private String Tempcode;
Private String Temptitle;
Private String tempcontent;
Private Date updatedate;
Public String Getlid () {
return lid;
}
.....
Adding and removing changes and checking operation
Before you do this, introduce the classes in spring that manipulate MongoDB.
As you can see in the previous configuration, we have introduced a class: Mongotemplate
Import Org.springframework.data.mongodb.core.MongoTemplate;
The packages referenced therein
Import Org.springframework.data.mongodb.core.query.Criteria;
Import Org.springframework.data.mongodb.core.query.Query;
Import org.springframework.data.mongodb.core.query.Update;
Import Org.springframework.stereotype.Service;
Import Com.alibaba.fastjson.JSON;
Import Com.mongodb.BasicDBObject;
So in our service layer, using annotations to use this class directly
@Autowired
protected mongotemplate mongotemplate;
New
New is very simple, the new object set the good value, directly save
mgvoice Mgvoice = new Mgvoice ();
Mgvoice.set ....//
Mongotemplate.save (Mgvoice);
Modify. (Modify the object's immediate properties)
{"CID": 1100658,//coach id "Voicetype": "F",//voice type F female male "voicespeed": 30,//speech Speed "voice Librarys ": [{" Lid ":" p2_b9dc7616-b476-40ee-8fd7-982711cece98 ",//Voice ID" type ": 2,//Voice Type 2 subjects 23 accounts three "title": "Uphill Start",//title "content": "Uphill start and fixed parking",//Content "is Temp ": 1,//whether the template 0 is not 1 is" Tempcode ":" K2_SPQB ",//template code, is also the template picture title" Temptitle ":" Uphill Start ",//
Template title "Tempcontent": "Uphill start and fixed parking",//template content, for restore default use "Updatedate": "2016-04-28 00:00:00"
}, {"Lid": "P3_ead65a9a-c659-4f55-a7d4-067d8935ee19", "type": 3, "title": "Pull Over", "content": "Please Pull Over", "istemp": 1, "Tempcode": "K3 _KBTC "," Temptitle ":" Pull Over "," tempcontent ":" Please Pull Over "," updatedate ":" 2016-0
4-28 00:00:00 " }]
}
We want to modify the Voicetype and Voicespeed property codes in the object
as follows
@Override public
void Updatecfg (Long CID,
Integer speed,
String type) {
criteria = Criteria.where ("CID"). is (CID);
Query query = new query (criteria);
Update update = Update.update ("Voicetype", type). Set ("Voicespeed", speed);
Mongotemplate.updatefirst (query, update, mgvoice.class);
}
Modify. (Nested objects within an object)
We want to modify the title and content properties in the object of lid = "P2_b9dc7616-b476-40ee-8fd7-982711cece98" in the Voicelibrarys array. Using Set
is to modify
query query = new query (Criteria.where ("CID"). Is (CID), and ("Voicelibrarys.lid"). is (lid));
Update update = Update.update ("Voicelibrarys.$.title", title). Set ("Voicelibrarys.$.content", content);
Mongotemplate.updatefirst (query, update, mgvoice.class);
If you want to voicelibrarys this array again, add an object to use Addtoset
The ID of the voice is empty is new
lid = Getcreateid (type);
Mgvoicelibrary voice = new Mgvoicelibrary ();
Voice.setlid (lid);
Voice.settype (type);
Voice.settitle (title);
Voice.setcontent (content);
Voice.setistemp (0);
Voice.setupdatedate (Rdate.getcurrentdate ());
Query query = query.query (Criteria.where ("CID"). Is (CID));
Update update = new update ();
Update.addtoset ("Voicelibrarys", voice);
Mongotemplate.upsert (query, update, mgvoice.class);
If you want to delete an object in the array Voicelibrarys, the object lid= "P2_b9dc7616-b476-40ee-8fd7-982711cece98". Using the Pull Property
Delete the ID of the speech cid record plid a tag in the array of
query query = query.query (Criteria.where ("CID"). Is (CID));
Basicdbobject s = new Basicdbobject ();
S.put ("lid", plid);
Update update = new update ();
Update.pull ("Voicelibrarys", s);
Mongotemplate.updatefirst (query, update, mgvoice.class);
In addition to the above operations there are some properties, such as unset.
If Update.upset an array. The object is emptied, but the array size does not change.
Modifies
Use Update.modifies (key). It will remove all values except the ID in this object.
If the object is this. Voicelibrarys is an object, not an array. The title and content objects are also modified.
{
"cid": 1100658,//Coach ID
"Voicetype": "F",//voice type F female male
"voicespeed": 30,//Voice speed
"Voicelibrarys":
{
"lid": "p2_b9dc7616-b476-40ee-8fd7-982711cece98",//Voice ID
"type": 2,//speech Type 2 account 23 account three
"title": " Uphill start ", //title
" Content ":" Uphill start and fixed parking ",//Content
" Istemp ": 1, //is the template 0 is not 1 is
" Tempcode ":" K2_SPQB ",// Template code , also template picture title
"Temptitle": "Uphill Start",//Template title
"tempcontent": "Uphill start and fixed parking",//template content for restore default use
" Updatedate ":" 2016-04-28 00:00:00 "
}
}
Code
is to modify
query query = new query (Criteria.where ("CID"). Is (CID));
Update update = Update.update ("Voicelibrarys.title", title). Set ("Voicelibrarys.content", content);
Mongotemplate.updatefirst (query, update, mgvoice.class);
If you want to delete the entire record. Use the Remove
@Override Public
Boolean removevoice (Long CID) {
//delete Voice
query query = query.query (Criteria.where ("CID"). Is (CID));
Mongotemplate.remove (query, mgvoice.class);
return true;
}
Here is a simple introduction to some basic methods of operation.
I just looked at it briefly. The code in the Spring MongoDB source package. The source package is also not written in detail. Just give the other document address. For example
/**
* Update using the {@literal $pull} update modifier
*
* @see http://docs.mongodb.org/manual/reference/ operator/update/pull/
* @param key
* @param value
* @return
*
/Public Update pull (String key, Object value) {
addmultifieldoperation ("$pull", key, value);
return this;
}
Under
http://docs.mongodb.org/manual/reference/operator/update/pull/
Then look at some basic ways to use it.