I am trying to find all documents and publish at most 5 from the results.
Following this section of the MongoDB doc, I am trying to do this:
Meteor.publish('teams', function () {
return Teams.find().limit(5);
});
Yet, in the server console, I get an exception:
Exception from sub teams id Pm6jKL8Sv3FSDSTfM TypeError: Object [object Object] has no method 'limit'
The following works fine:
Meteor.publish('teams', function () {
return Teams.find({}, {limit:5});
});
Why does the second way work, rather than the first? And where can I find documentation for it?
Meteor's collection API is somewhat different from that of the mongo API. find takes up to two parameters: a selector object, and an options object. options allows you to specify such things as sort, skip, limit and fields, in addition to the meteor-specific reactive and transform.
Related
What I intend to achieve is some sort of "live query" functionality.
So far I've tried using the "watch" method. According to the documentation:
You can open a stream of changes that match a filter by calling
collection.watch(delegate:) with a $match expression as the argument.
Whenever the watched collection changes and the ChangeEvent matches
the provided $match expression, the stream’s event handler fires with
the ChangeEvent object as its only argument
Passing the doc ids as an array works perfectly, but passing a query doesn't work:
this.stitch.db.collection<Queue>('queues')
.watch({
hospitalId: this.activehospitalid
}));
I've also tried this:
this.stitch.db.collection<Queue>('queues')
.watch({
$match: {
hospitalId: this.activehospitalid
}
},
));
Which throws an error on the console "StitchServiceError: mongodb watch: filter is invalid (unknown top level operator: $match)". The intention is watch all documents where the field "hospitalId" matches the provided value, or to effectively pass a query filter to the watch() method.
After a long search I found that it's possible to filter, but the query needs to be formatted differently
this.stitch.db.collection<Queue>('queues')
.watch({
$or: [
{
"fullDocument.hospitalId": this.activehospitalid
}
]
},
));
For anyone else who might need this, please note the important fullDocument part of the query. I havent found much documentation relating to this, but I hope it helps
In my MongoDB backend I am using pre and post hooks to compare pre-save and post-save versions of a document, in order to handle post-processing when certain changes occur. I am also using underscore's _.difference() method to find the differences between the two versions of the document.
However, because these are not simple objects, but actually Mongoose objects, there is all sorts of additional data mixed in -- such as:
activePaths:
StateMachine {
paths: [Object],
states: [Object],
stateNames: [Array],
map: [Function] },
pathsToScopes:
So, long story short, I'm trying to figure out how to just get the object data - nothing else, so that _.difference will pull out the data that's changed. I tried using JSON.parse() but this didn't work in my case. I also tried calling the lean() Mongoose method on my two docs, but this caused a "not a function" error.
You can use the toObject() method on a document to convert it to a regular object.
The .lean() method can only be called on queries beforehand like this:
// passing options (in this case return the raw js objects, not mongoose documents by passing `lean`
Adventure.findById(id, 'name', { lean: true }, function (err, doc) {});
// same as above
Adventure.findById(id, 'name').lean().exec(function (err, doc) {});
I'm new to Meteor. I've been stuck on this problem for a while. I can successfully adds items to a collection and look at them fully in the console. However, I cannot access all of the read operations in my .js file.
That is, I can use .find() and .findOne() with empty parameters. But when I try to add .sort or an argument I get an error telling me the object is undefined.
Autopublish is turned on, so I'm not sure what the problem is. These calls are being made directly in the client.
This returns something--
Template.showcards.events({
"click .play-card": function () {
alert(Rounds.find());
}
})
And this returns nothing--
Template.showcards.events({
"click .play-card": function () {
alert(Rounds.find().sort({player1: -1}));
}
})
Sorry for the newbie question. Thanks in advance.
Meteor's collection API works a bit differently from the mongo shell's API, which is understandably confusing for new users. You'll need to do this:
Template.showcards.events({
'click .play-card': function() {
var sortedCards = Rounds.find({}, {sort: {player1: -1}}).fetch();
console.log(sortedCards);
}
});
See this for more details. Also note that logging a cursor (the result of a find) probably isn't what you want. If you want to see the contents of the documents, you need to fetch them.
Rounds.find().sort({player1: -1}) returns a cursor, so you will want to do this:
Rounds.find().sort({player1: -1}).fetch();
Note that this returns an Array of document objects. So you would do something more like this:
docs = Rounds.find().sort({player1: -1}).fetch();
alert(docs[0]);
I'm using DoctrineMongoDBBundle with Symfony2 and I've a problem with geocoordinates. This works fine but when the longitude is for example like that: 0.635467 the code doesn't work. I have more geocoordinates and only fails when it begins with 0. and the distance field is NULL.
This is my code:
$locations = $dm->createQueryBuilder('MyBundle:Location')
->field('id')->in($arrayIds)
->field('geocoordinates')
->geoNear($geocodes['lat'],$geocodes['lon'])
->getQuery()->execute()->toArray();
I'm following this link: http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/geospatial-queries.html but with the geonear method.
The geoNear() query builder method is not intended to be used on a field. near() is the builder method that would follow a field() focus. You can see what both of these builder methods do in Builder.php within the doctrine/mongodb project. Note that geoNear() changes the query type (similar to what update() does). The query type is then checked in Query.php (follow the switch statement) and determines how we issue the query on the collection. Some are actual query operations, but things like map/reduce and geoNear are commands.
See if the following code works:
$dm->createQueryBuilder('MyBundle:Location')
->geoNear($geocodes['lat'],$geocodes['lon'])
->field('id')->in($arrayIds)
->getQuery()->execute()->toArray();
If not, please debug the values that Query.php passes to the Collection::near() method. Alternatively, you can debug the entire query array generated by the builder by using the Query::getQuery() method.
I'm trying to do something similar to the following
http://mongoosejs.com/docs/embedded-documents.html
However instead of new BlogPost I'm trying to retrieve then add as follows
function addComment(id, comment, callback) {
Post.findOne(id, function(err, post) {
post.comments.push(comment);
});
}
I'm getting
TypeError: Cannot call method 'call' of undefined
What am I doing wrong? I left out the saving code for simplification it is crashing even without trying to save.
Assuming that the id parameter is an ObjectId or string (and not a query object), you should be calling findById instead of findOne.