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

Oracle - duplicate fields in RECORD,TABLE or argument list are not permitted

Oracle - ORA-29282: invalid file ID, ORA-06512: at "SYS.UTL_FILE"