Mongo java driver with unwind on 3.1.0

593 views
Skip to first unread message

vwko...@gmail.com

unread,
Oct 14, 2015, 2:50:19 PM10/14/15
to mongodb-user
i have a object similar to:

{
    "data" : {
          "link" : [
                 {
                      ....
                  },
                  {
                       .....
                  }
          ]
    }
}

And i can run a command like:

    db.mydata.aggregate( { $unwind : "$data.link" }, { $match : { "data.link.somedata" : "something" } }

to return data i want from the mongo shell but when i try to do it with the java driver:
 
        Bson                 unwind    = Aggregates.unwind( "$data.link" );
        Bson                 match     = Aggregates.match( eq( "data.link.somedata", "something" ) );
        List                    pipeline  = Arrays.asList( unwind, match );
       
        Collection<Document> documents = this.getCollection().aggregate( pipeline ).into( new ArrayList<Document>() );

it throws an error:

Value at end of $unwind field path '$data.link' must be an Array, but is a Object

Can anyone help?

Jeff Yemin

unread,
Oct 14, 2015, 5:01:23 PM10/14/15
to mongod...@googlegroups.com
I'm not able to reproduce this using the 3.1.0 Java driver.

Given the following dataset (using shell syntax):

{ "_id" : ObjectId("561ebe3eabe46b29fbeb8749"), "data" : { "link" : [ { "somedata" : 1 }, { "somedata" : 2 } ] } }
{ "_id" : ObjectId("561ebe88abe46b29fbeb874a"), "data" : { "link" : [ { "somedata" : 1 }, { "somedata" : "something" } ] } }
{ "_id" : ObjectId("561ebe89abe46b29fbeb874b"), "data" : { "link" : [ { "somedata" : 1 }, { "somedata" : "something" } ] } }

and the following Java program:

import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.json.JsonMode;
import org.bson.json.JsonWriterSettings;
import java.util.ArrayList;
import java.util.List;

import static com.mongodb.client.model.Aggregates.match;
import static com.mongodb.client.model.Aggregates.unwind;
import static com.mongodb.client.model.Filters.eq;
import static java.util.Arrays.asList;

public class AggregatesTest {
public static void main(String[] args) {
Bson unwind = unwind("$data.link");
Bson match = match(eq("data.link.somedata", "something"));
List<Bson> pipeline = asList(unwind, match);

MongoClient c = new MongoClient();
MongoCollection<Document> collection = c.getDatabase("arrays").getCollection("mydata");

List<Document> results = collection.aggregate(pipeline).into(new ArrayList<Document>());

results.forEach(document -> System.out.println(document.toJson(new JsonWriterSettings(JsonMode.SHELL))));
    }
}
I get the following output:
{ "_id" : ObjectId("561ebe88abe46b29fbeb874a"), "data" : { "link" : { "somedata" : "something" } } }
{ "_id" : ObjectId("561ebe89abe46b29fbeb874b"), "data" : { "link" : { "somedata" : "something" } } }

Is it possible that some of the documents in your collection have data.link values that are not arrays?  


Regards,
Jeff


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/2638aba8-5694-4101-b7e1-0feda3bc1e85%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

vwko...@gmail.com

unread,
Oct 14, 2015, 5:07:20 PM10/14/15
to mongodb-user
Yes, it seems I was mis-configured to point to the wrong collection which did not have the array I was looking for. 

Thank you for your help.
Reply all
Reply to author
Forward
0 new messages