Creating a MongoDB View using the Java driver

1.6k views Asked by At

How do I create a view in mongo db which can expose all the documents from a collection? I have used following code:

java.util.List<Bson> tstL = new ArrayList<>();
db.createView("tst_view","collection_name",tstL);

I am trying it with empty list for pipeline but it doesn't work. Can you please help?

1

There are 1 answers

0
svaponi On
    database.createView("Tree", "Node", asList(
            new Document("$graphLookup",
                    new Document("from", "Node")
                            .append("startWith", "$childrenRefs")
                            .append("connectFromField", "childrenRefs")
                            .append("connectToField", "_id")
                            .append("as", "children")
            )
    ));