MongoDB crud Operations (Java Util)

Source: Internet
Author: User
Tags java util

1. Save Insert Operation:

 Public Static synchronized String Insert (DBObject record) {        = mongodb.getdbcollection (table_name);        Record.put (new  Date (). GetTime ());         = Col.insert (record);         // _ID of the currently inserted table        return record.get ("_id"). toString ();   }

Note: After the Col.insert (record) execution succeeds, the record can get _id by Record.get ("_id") whether it is empty to determine if the save is empty!

2, update the editing operation:

 Public Static synchronized intedit (DBObject record) {dbcollection col=mongodb.getdbcollection (table_name); //Check if there is a corresponding obj_id presenceBasicdbobject queryobj =NewBasicdbobject (); Queryobj.put ("_id", Record.get ("_id")); Dbcursor DC=Col.find (queryobj); if(Dc.count () < 1) {            returnno_record_exist; }         //Save DataRecord.removefield ("_id"); Writeresult result= Col.update (Queryobj,NewBasicdbobject ("$set", record)); returnResult.getn (); }

Note: Before updating the data to be updated in the data _id removed otherwise error, Queryobj is updated query conditions, record is updated records, can not contain _id in the record ...

3. Delete operation:

 Public Static synchronized intdel (dbobject record, olacloudcontent olacloudcontent) {dbcollection col=mongodb.getdbcollection (table_name); //Check if there is a corresponding obj_id presenceDbcursor DC =Col.find (record); if(Dc.count () < 1) {            returnno_record_exist; }//Delete DataWriteresult result =Col.remove (record); Oplog.addoplog (Olacloudcontent.getusername (), TABLE_NAME,"Del_key", record.tostring ()); returnResult.getn (); }    

4. Query operation:

 Public Static synchronizedCorpusanswerwrap Find (dbobject record,dbobject sort,intlimit) {Corpusanswerwrap Answerwrap=NewCorpusanswerwrap (); List<AnswerEntity> answerlist =NewArraylist<answerentity>(); Dbcollection Col=mongodb.getdbcollection (table_name); Dbcursor DC=Col.find (record). Sort (sort); if(Limit > 0) {dc.limit (limit);        } answerwrap.settotalcount (Dc.count ());  while(Dc.hasnext ()) {answerentity entity= Answerwrap.Newanswerentity ();            Entity.setrecord (Dc.next ());        Answerlist.add (entity); } answerwrap.setdatas (Answerlist.toarray (Newanswerentity[answerlist.size ()]); returnAnswerwrap; }

MongoDB crud Operations (Java Util)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.