Reputation: 239
Does anyone know if strongloop supports limit fields to return from a Query for MongoDB databases? I think strongloop doesnt support all the MongoDB query features so I'm afraid this might not be supported.
This is what I want to achieve using strongloop:
https://docs.mongodb.org/v3.0/tutorial/project-fields-from-query-results/
MongoDB operations example (executed within the database):
Without Limiting the Fields:
> db.Releases.find({epoch_time: {$gte: 1451417675, $lt: 1483717675}})
{ "_id" : ObjectId("5682bbcbab755688f9bfd939"), "release_id" : 3, "event_id" : 1, "epoch_time" : 1451417675 }
{ "_id" : ObjectId("5682bbccab755688f9bfd93a"), "release_id" : 4, "event_id" : 2, "epoch_time" : 1452717675 }
{ "_id" : ObjectId("5682bbccab755688f9bfd93b"), "release_id" : 5, "event_id" : 2, "epoch_time" : 1453717675 }
{ "_id" : ObjectId("5682bbccab755688f9bfd93c"), "release_id" : 6, "event_id" : 3, "epoch_time" : 1461207675 }
{ "_id" : ObjectId("5682bbccab755688f9bfd93d"), "release_id" : 7, "event_id" : 3, "epoch_time" : 1461407675 }
{ "_id" : ObjectId("5682bbccab755688f9bfd93e"), "release_id" : 8, "event_id" : 4, "epoch_time" : 1461417675 }
{ "_id" : ObjectId("5682bbccab755688f9bfd93f"), "release_id" : 9, "event_id" : 1, "epoch_time" : 1472717675 }
{ "_id" : ObjectId("5682bbd1ab755688f9bfd940"), "release_id" : 10, "event_id" : 5, "epoch_time" : 1473717675 }
Limiting the fields:
> db.Releases.find({epoch_time: {$gte: 1451417675, $lt: 1483717675}},{_id:0,release_id:0,event_id:0})
{ "epoch_time" : 1451417675 }
{ "epoch_time" : 1452717675 }
{ "epoch_time" : 1453717675 }
{ "epoch_time" : 1461207675 }
{ "epoch_time" : 1461407675 }
{ "epoch_time" : 1461417675 }
{ "epoch_time" : 1472717675 }
{ "epoch_time" : 1473717675 }
I tried something like this in StrongLoop but I still get all the fields in the query.
Release.find({where: {...query expression...} },
{_id:0,release_id:0,event_id:0}, //trying to limit return fields
function (err, releases) {
...
}
);
Thanks, Carlos
Upvotes: 2
Views: 193
Reputation: 102
Creating a function inside our models
MyModel.observe('access', function logQuery(ctx, next) {
Release.find({
where: {
release_id: 4
}
});
});
Upvotes: 0
Reputation: 1648
Yes, loopbackjs supports limiting fields in query. Diffrence between limiting fileds in mongodb and loopback query is that loopbackjs defaults return all fields to false when using fields filter. You have to explicitly say which fields you want query to return.
Release.find({where: {release_id : 4}, fields: {epoch_time: true}}, //return only epoch_time
function (err, releases) {
...
}
);
Upvotes: 2
Reputation: 2124
According to the strongloop docs - https://docs.strongloop.com/display/public/LB/Querying+data
It provides ability to add multiple options for querying, filtering, limiting data on both REST endpoints as well as in Model Methods.
You can limit the fields in output in a remote method like this -
Release.find(
{ fields : {"epoch_time":true}
}, function(err, releases) {
// ... your code here
} );
Or you can do it in operation hooks like this as specified in https://docs.strongloop.com/display/public/LB/Operation+hooks -
MyModel.observe('access', function logQuery(ctx, next) {
console.log('Accessing %s matching %s', ctx.Model.modelName, ctx.query.where);
next();
});
Upvotes: 0