Created
May 6, 2014 13:20
-
-
Save minisu/11561753 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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