Add, delete, modify, and query Databases
Next, we can read and write the database through the driver.
Before getting data, we need to get the collection mapped to the corresponding class: Dbhelper. DB. getcollection <t> (name );//Name indicates the collection name mapped to T.
The name parameter must be specified for the getcollection method. In fact, I think the collection name corresponding to T should have been determined before the ing. Why must the name be specified here? Or are there other methods I don't know?
The read data of MongoDB is the find command. The find/findall method is also used in the driver.
Getcollection <t> (). findall ();
In practice, we usually have query conditions and sorting conditions.
The MongoDB query condition is imongoquery and the sorting condition is imongosortby.
Getcollection <t> (). Find (query). setsortorder (sortby );
You can also use setskip and setlimit for paging:
Getcollection <t> (). Find (query). setsortorder (sortby). setskip (skip). setlimit (Limit );
The following describes how to obtain the corresponding data based on tags and sort the data based on the Update time. The paging function is not available yet:
/// <Summary>
/// Get tag data
/// </Summary>
/// <Param name = "ht"> </param>
/// <Returns> </returns>
Public Static List {
Querycomplete query = Null ;
If (Ht! = Null )
{
Query = query. And (
Query. GTE (housetag. pnamearea, HT. areamin ),
Query. Lte (housetag. pnamearea, HT. areamax ),
Query. GTE (housetag. pnameprice, HT. pricemin ),
Query. Lte (housetag. pnameprice, HT. pricemax ),
Query. GTE (housetag. pnamepricetotal, HT. pricetotalmin ),
Query. Lte (housetag. pnamepricetotal, HT. pricetotalmax ),
Query. GTE (housetag. pnameyear, HT. yearmin ),
Query. Lte (housetag. pnameyear, HT. yearmax)
);
If (! String . Isnullorempty (HT. Zone ))
{
MongoDB. bson. bsonregularexpression Reg = New MongoDB. bson. bsonregularexpression ( String . Format ( " * {0 }* " , HT. Zone ));
Query = query. And (query, query. Matches (housetag. pnamezone, REG ));
}
}
Return Revoke helper. findall " Updatetime " ). Tolist ();
}
After obtaining the data, the next step is to save the data. It is easier to save (ADD and modify) The data. You can simply use save. Besides, I have not processed the changed data here, every time the object is saved, the whole object is updated (efficiency is definitely not suitable, but programming is more convenient than a little ):
/// <Summary>
/// Update a data set
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "listentity"> </param>
Public Static Void Save <t> (ienumerable <imongoentity> listentity)
Where T: Class , Imongoentity
{
If (Listentity! = Null )
{
Collect collection <t> Col = getcollection <t> ();
Foreach (Imongoentity entity In Listentity)
{
Col. Save (entity );
}
}
}
The difference between data deletion and data update is that you need to obtain the data based on the corresponding conditions (imongoquery ):
/// <Summary>
// Delete a data set
/// </Summary>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "listentity"> </param>
Public Static Void Remove <t> (ienumerable <imongoentity> listentity)
Where T: Class , Imongoentity
{
If (Listentity! = Null )
{
Collect collection <t> Col = getcollection <t> ();
Foreach (Imongoentity entity In Listentity)
{
Col. Remove (query. eq ( " ID " , Entity. ID ));
}
}
}