Skip to content

Instantly share code, notes, and snippets.

@minisu
Created May 6, 2014 13:20
Show Gist options
  • Save minisu/11561753 to your computer and use it in GitHub Desktop.
Save minisu/11561753 to your computer and use it in GitHub Desktop.
package com.smartbear.saas.rs.storage;
import com.github.fakemongo.Fongo;
import com.mongodb.util.JSONSerializers;
import org.jongo.Jongo;
import org.jongo.MongoCollection;
import org.junit.Test;
import static java.util.stream.Collectors.joining;
public class MongoTest
{
@Test
public void projectTest()
{
/*
Get started with `help`
To try out an interactive tutorial type `tutorial`
> db.foo.insert( {bar: "bazz"} )
> db.foo.aggregate( [ {$project: { bla: {$divide: [4,2]} } } ] )
{
"ok" : 1,
"result" : [
{
"_id" : ObjectId("5368e0011cdcaf5f6a800b03"),
"bla" : 2
}
]
}
*/
Fongo fongo = new Fongo( "fongo" );
Jongo jongo = new Jongo( fongo.getMongo().getDB( "myDb" ) );
MongoCollection collection = jongo.getCollection( "foo" );
collection.insert( "{bar: 'bazz'}" );
String result = collection
.aggregate( "{ $project: { bla: {$divide: [4,2]} } }" )
.map( r -> JSONSerializers.getStrict().serialize( r ) )
.stream()
.collect( joining( "," ) );
System.out.println( result ); // { "_id" : { "$oid" : "5368e0f3cf5a47d5a22d7b75"}}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment