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)