[cbmongodb] How to limit the fields returned from find()?

Reading here https://docs.mongodb.org/manual/tutorial/project-fields-from-query-results/ seems possible to limit the fields returned by a query.

Is this possible with cbMongoDb?

It is but, once again, you’d have to use the Collection find( criteria, options ) method directly and pass the restrictions. A CBMongoDB example, using the Mongo docs example schema would be:

var foodClassification = foodModel.getCollectionObject.find(
    { "type" : "food", "_id" : 3 },
    { "classification" : { "category" : "chocolate" } }
);

A more elegant way to do customize the result documents is by using aggregation:

I find the aggregation syntax much more natural and expressive.

HTH, Jon

Sorry forgot to double-check my code example. Should be:

var foodClassification = foodModel.getCollectionObject().find(
    { "type" : "food", "_id" : 3 },
    { "classification" : { "category" : "chocolate" } }
);

Hi Jon,

not sure if this is what I’m looking for.

Let’s say I have a model with name,surname,address properties.

I want to output only name and surname. I don’t want to retrieve address.

var users = userModel.getCollectionObject().find(
    { "name" : "Jon", "surname": "Clausen" },
    { "address" : 0 }
);

Will this works?

Seems that what I’m tryng to do is not possible. I’ve tried also:

`

util = getModel(“mongoutil@cbmongodb”);

include = util.toMongo({“name”:“Jon”,“surname”:“Clausen”});
exclude = util.toMongo({“address”:JavaCast(“boolean”,0)});

result = getModel(“mongoclient@cbmongodb”).getDBCollection(“users”).find(
include,
exclude
);

`

Tropicalista,

My apologies. I gave you the wrong info, from a previous release. I forgot that, as of v3.0, the find() method doesn’t accept a projection as the second argument. You need to use the cursor to do that, like so:

var util = getModel("mongoutil@cbmongodb");

var criteria = {"name":"Jon","surname":"Clausen"};
var projection = util.toMongo({"address":0});

var result = getModel("User")
            .getCollectionObject()
            .find( include , { "limit": 1 } )
            .getResult()
            .projection( projection )
            .iterator()
            .tryNext();

I just pushed up a patch that I’ll probably bump to a release later in the week, that allows you to pass a projection key to the find() options. What those options do is perform the cursor operations automatically and the update will allow you use the encapsulated result, instead of the cursor object:

var criteria = {"name":"Jon","surname":"Clausen"};
//note we don't need util to cast this anymore
var projection = {"address":0};

var result = getModel("User")
            .getCollectionObject()
            .find(  
                    criteria=include ,  
                    options={   
                        "projection": projection  
                    }  
                );

If you want to grab a copy of the latest version of MongoCollection, you can use the updated options in your project: cbmongodb/Collection.cfc at development · jclausen/cbmongodb · GitHub

Lastly, you shouldn’t javacast your projection includes/excludes to booleans. Those should be bits or integers.

Jon

Thanks Jon, this works great.

Hi Jon,

another question.

If I want retrieve all record in db, how can I do?

I have tried with:

var util = getModel("mongoutil@cbmongodb");

var projection = util.toMongo({"address":0});

var result = getModel("User")
            .getCollectionObject()
            .find( {} , { "limit": 10 } )
            .getResult()
            .projection( projection )
            .iterator()
            .tryNext();

but this giges me only one record.

Omit the “tryNext”. That is the first record.