View's Query Issue

Why the below filter based on the document property doesn’t work?
It always return everything!

public static Query getData(Database database, final String type) {
        View view = database.getView("data");
        if (view.getMap() == null) {
            view.setMap(new Mapper() {
                @Override
                public void map(Map<String, Object> document, Emitter emitter) {
                    if(String.valueOf(document.get("docType")).equals(type)){
                        emitter.emit(document.get("_id"), null);
                    }
                }
            }, "4");
        }
        return view.createQuery();
}

That’s not a legal map function — it’s using a value from the outside (type). A map function has to be a pure function whose behavior depends only on its input (the document parameter.)

The map function runs while indexing new documents. It does not run at query time. Our documentation goes into detail on this.

@jens Are you saying that I will need one function for each type?

No. You’re building an index. In the map function, emit the document’s type as the key. Then when querying, set the query’s key to the type you want to search for.

Again, please read the documentation describing how views and queries work. It’s different than what you’re used to.

You may want to look at this documentation for a primer on how views work.

It is my first time working with a NoSQL database, I will take a look to find out what is wrong!
Thank you