Java MongoDB - Commands

Queries only the "productID" and "@type" fields from all the collection documents:

BasicDBObject fields = new BasicDBObject();
query.put("productID", 1);
query.put("@type", 2);

BasicDBObject allQuery = new BasicDBObject();

DBCursor cursor = mongoDatasource.getMongoDatasource().find(allQuery, fields);

while(cursor.hasNext()) {
DBObject document = cursor.next();
System.out.println(document);
}

Result:
{ "_id" : { "$oid" : "56c6c9e0612eb61e88166894"} , "@type" : "Product" , "productID" : "1"}

Adding a new node to an exisitng array

BasicDBObject query = new BasicDBObject().append("productID", String.valueOf(productId));
DBObject listItem = new BasicDBObject("offers.offers", newValue);
BasicDBObject update = new BasicDBObject().append("$push", listItem);
mongoDatasource.getMongoDatasource().update(query, update, false, true);

Comments

Popular posts from this blog

Hibernate - HibernateException: createQuery is not valid without active transaction