package mongodb;
import java.util.ArrayList;
import java.util.List;
import java.net.UnknownHostException;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoCredential;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
public class MongdbUpdate {
public static void main(String[] args) {
// TODO Auto-generated method stub
try
{
// 認證
MongoCredential journaldevAuth = MongoCredential.createMongoCRCredential("username", "database", "password".toCharArray());
List auths = new ArrayList();
auths.add(journaldevAuth);
//連線
ServerAddress serverAddress = new ServerAddress("your server address", your port);
MongoClient mongo = new MongoClient(serverAddress,auths);
//選擇連線database
DB db = mongo.getDB("database");
//選擇collection (table)
DBCollection site = db.getCollection("site");
// $set
BasicDBObject query = new BasicDBObject();
query.put("city", "taipei");
BasicDBObject newDocument = new BasicDBObject();
newDocument.put("num", 125);
BasicDBObject updateObj = new BasicDBObject();
updateObj.put("$set", newDocument);
site.update(query, updateObj, true, true);
// $inc
updateObj.clear();
updateObj.put("$inc", newDocument);
site.update(query, updateObj, true, true);
}
catch(UnknownHostException ex)
{
}
}
}
2015-09-15
MongoDB using Java part 1 - update
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment