Showing posts with label MongoDB. Show all posts
Showing posts with label MongoDB. Show all posts

Wednesday 2 October 2013

How would I do $push using MongoRepository in Spring Data?

Yes, you must implement a custom method on the repository and your push method would be something like this :


public class FooRepositoryImpl implements
    AppointmentWarehouseRepositoryCustom {

    @Autowired
    protected MongoTemplate mongoTemplate;

    public void pushMethod(String objectId, Object... events) {
        mongoTemplate.updateFirst(
            Query.query(Criteria.where("id").is(objectId)), 
            new Update().pushAll("events", events), Foo.class);
    }
}
You can do this but I ran into an issue where the "_class" field wasn't being preserved. 
The pushed object itself was run through the configured converter but for some reason the "_class" field of that pushed object wasn't written. 
However, if I injected the converter and wrote the object to a DBObject myself, then the "_class" field was preserved and written. The thus becomes:

public class FooRepositoryImpl implements
AppointmentWarehouseRepositoryCustom {

@Autowired
protected MongoTemplate mongoTemplate;

public void pushMethod(String objectId, Object event) {
    DBObject eventObj = new BasicDBObject();
    converter.write(event, eventObj);
    mongoTemplate.updateFirst(
        Query.query(Criteria.where("id").is(objectId)), 
        new Update().push("events", eventObj), Foo.class);
  }
}

Friday 5 July 2013

NoSQL, DBRefs : MongoDB

> db.customers.findOne()
{
        "_id" : ObjectId("4ef4a61a90eec3e3c748263c"),
        "uid" : 1,
        "name" : "Andrey",
        "lastname" : "Knupp Vital"
}
> db.orders.findOne()
{
        "_id" : ObjectId("4ef4a66490eec3e3c748263d"),
        "oid" : 1,
        "uid" : 1,
        "price" : "149.90"
}
> db.orders.remove()
> order = { oid : 1 , price : 149.90 , uid : new DBRef ( 'customers' , ObjectId("4ef4a61a90eec3e3c748263c") ) } ;
{
        "oid" : 1,
        "price" : 149.9,
        "uid" : {
                "$ref" : "customers",
                "$id" : ObjectId("4ef4a61a90eec3e3c748263c")
        }
}
> db.orders.save(order)
> order.uid.fetch()
{
        "_id" : ObjectId("4ef4a61a90eec3e3c748263c"),
        "uid" : 1,
        "name" : "Andrey",
        "lastname" : "Knupp Vital"
}

Tuesday 25 June 2013

MongoDB Get names of all keys in collection

mr = db.runCommand({ 

 "mapreduce" : "things", 

 "map" : function() {

   for (var key in this) { 

         emit(key, null); } },

 "reduce" : function(key, stuff) { 

  return null; }, "out": "things" + "_keys" })

Then run distinct on the resulting collection so as to find all the keys:

db[mr.result].distinct("_id") ["foo", "bar", "baz", "_id", ...]

Wednesday 19 June 2013

Export MongoDB "Database" in JAVA

public class TempMongoExportDB {

public void mongoExport()
throws IOException {

String command = "mongodump -db tree -o c:\\backup";
Process p = Runtime.getRuntime().exec(command);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(
p.getInputStream()));

BufferedReader stdError = new BufferedReader(new InputStreamReader(
p.getErrorStream()));

// read the output from the command
String s = "";

while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}

while ((s = stdError.readLine()) != null) {
System.out.println("Std ERROR : " + s);
}

}

public static void main(String[] args) throws IOException {

TempMongoExportDB tempMongoExportDB = new TempMongoExportDB();
tempMongoExportDB.mongoExport();

}

}