import java.util.Random;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
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();
for (int i = 0; i < 10; i++) {
collection
.insert(new BasicDBObject("x", new Random().nextInt(100)));
}
DBCursor find = collection.find();
try {
while (find.hasNext()) {
DBObject document = find.next();
System.out.println(document);
}
} finally {
find.close();
}
DBObject findOne = collection.findOne();
System.out.println(findOne);
long count = collection.count();
System.out.println(count);
client.close();
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.