import java.util.Arrays;
import org.bson.types.ObjectId;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import com.mongodb.ServerAddress;
public class MongoDB {
public static void main(String[] args) throws UnknownHostException {
MongoClient client = new MongoClient(new ServerAddress("localhost",
27017));
DB database = client.getDB("databaseName");
DBCollection collection = database.getCollection("collectionName");
collection.drop();
DBObject document01 = new BasicDBObject().append("a", 1);
System.out.println(document01);
collection.insert(document01);
System.out.println(document01);
DBObject document02 = new BasicDBObject("_id", new ObjectId()).append(
"b", 2);
System.out.println(document02);
collection.insert(document02);
System.out.println(document02);
DBObject document03 = new BasicDBObject("c", 3);
DBObject document04 = new BasicDBObject();
System.out.println(document03);
System.out.println(document04);
collection.insert(Arrays.asList(document03, document04));
System.out.println(document03);
System.out.println(document04);
client.close();
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.