Collection.update giving server error - coffeescript

Trying to learn the Meteor framework as well as coffeescript/node all at once. Been working on a simple file upload program that utilizes onloadend. When the FileReader onloadend event function is called I try to determine if the file already exists and if so I update with the new file data and version.
The code works for insert but not update. Can someone help? I've posted to meteor-talk w/o an answer as I suspect its the weekend (when I do most of my experimentation).
Code snippet...
file_reader.onloadend = ((file_event) ->
(event) ->
f_filename = escape file_event.name
version = 0
f_record = null
f_record = doc_repo.findOne { name: f_filename }
if f_record && f_record.name
doc_repo.update
name: f_filename
,
$set:
version: 10
else
doc_repo.insert
name: f_filename
data: event.target.result
version: 0
)(file_obj)
Error
Exception while invoking method '/documents/update' TypeError: Cannot read property 'toBSON' of undefined
at Function.calculateObjectSize (/usr/local/meteor/lib/node_modules/mongodb/node_modules/bson/lib/bson/bson.js:210:12)
at BSON.calculateObjectSize (/usr/local/meteor/lib/node_modules/mongodb/node_modules/bson/lib/bson/bson.js:1463:15)
at UpdateCommand.toBinary (/usr/local/meteor/lib/node_modules/mongodb/lib/mongodb/commands/update_command.js:67:20)
at Connection.write (/usr/local/meteor/lib/node_modules/mongodb/lib/mongodb/connection/connection.js:138:40)
at __executeInsertCommand (/usr/local/meteor/lib/node_modules/mongodb/lib/mongodb/db.js:1837:14)
at Db._executeInsertCommand (/usr/local/meteor/lib/node_modules/mongodb/lib/mongodb/db.js:1912:7)
at Collection.update (/usr/local/meteor/lib/node_modules/mongodb/lib/mongodb/collection.js:445:13)
at app/packages/mongo-livedata/mongo_driver.js:178:16
at Db.collection (/usr/local/meteor/lib/node_modules/mongodb/lib/mongodb/db.js:507:44)
at _Mongo._withCollection (app/packages/mongo-livedata/mongo_driver.js:51:13)

It looks like Mongo is not getting the second parameter which it needs to do the update. So in regular JavaScript it's expecting this:
collection.update({..selector..}, { .. modifier });
So I'd try putting some curly brackets around the modifier object, like this:
doc_repo.update
name: f_filename,
{
$set:
version: 10
}

Related

Syntax error in db.collection.save()

I am trying MongoDB for first time but I am stuck on the following syntax error: unexpected token illegal. I checked it and it looks OK. I found similar a problem on StackOverflow but that is for a different error. What am I doing wrong?
Here is my script:
db.student.save({"_id":ObjectId(5983548781331adf45ec7),"name":"replaced","age":55})
The problem is here : ObjectId(5983548781331adf45ec7)
ObjectId accepts a string of 24 hex digits.
ObjectId("<24 hex digits here>")
E.g. ObjectId("0123456789abcdef01234567")
If you are using a backend source code such as groovy (with Grails GORM), you can try the following script which is very clean and readable:
def studentFromDB = db.student.findById("5983548781331adf45ec7")
studentFromDB.name = "replaced"
studentFromDB.age = 55
studentFromDB.save(flush: true, failOnError: true)
If you use it directly onto the mongo you should use the MongoDB update, for example:
db.student.update(
{"_id":ObjectId(5983548781331adf45ec7) },
{
name: "replaced",
age: 55
},
{ upsert: true }
)
I fixed it...the error is i missed quotes inside 'ObjectId'
the corrected script is `db.student.save({"_id":ObjectId("57fcf46763ecce707f071884"),"name":"rep_dsave","age":37}).
Thanks 4J41 and rotemy
`

Having Difficulty Using MongoDb C# Driver's Sample()

I am trying to get some random items from the database using the Sample() method. I updated to the latest version of the driver and copied the using statements from the linked example. However, something isn't working, and I am hoping it's some simple mistake on my part. All the relevant information is in the image below:
Edit:
Greg, I read the aggregation docs here and the raw db method doc here, but I still don't get it. Last two lines are my attempts, I have never used aggregation before:
var mongoCollection = GetMongoCollection<BsonDocument>(collectionName);
long[] locationIds = new long[] { 1, 2, 3, 4, 5 };
var locationsArray = new BsonArray();
foreach (long l in locationIds) { locationsArray.Add(l); };
FilterDefinition<BsonDocument> sampleFilter = Builders<BsonDocument>.Filter.In("LocationId", locationsArray);
var findSomething = mongoCollection.Find(sampleFilter); //this works, the two attempts below don't.
var aggregateSomething = mongoCollection.Aggregate(sampleFilter).Sample(25);
var aggregateSomething2 = mongoCollection.Aggregate().Match(sampleFilter).Sample(25);
Sample is only available from an aggregation. You need to start with Aggregate, not Find. I believe it's also available in Linq.
UPDATE:
Looks like we don't have a method for it specifically. However, you can use the AppendStage method.
mongoCollection.Aggregate(sampleFilter)
.AppendStage<BsonDocument>("{ $sample: { size: 3 } }");

Console log in MongoDB Shell

I want to write functions into MongoDB Shell like this:
var last = function(collection) { db[collection].find().sort({_id: -1}).limit(1).toArray(); }
But there is one problem. When I call last() function, it will make no output. How to fix it?
You need to use either use the JavaScript print() function or the mongo specific printjson() function which returns formatted JSON to actually log to output the result from the find method, for example:
var last = function(collection) {
var doc = db.getCollection(collection).find().sort({_id: -1}).limit(1).toArray();
printjson(doc);
};
last("test");

Cloud9 & Meteor.js undefined global var

I'm trying to create the Leaderboard on Cloud9. But I get the error: PlayersList not defined... in the editor. The app is working, but then it's code in editor underlining all 'not defind PlayersList'
The code:
PlayersList = new Mongo.Collection('players');
if(Meteor.isClient){
Template.leaderboard.helpers({
'player': function(){
return PlayersList.find({}, {sort: {score: -1, name: 1}});
},
'selectedClass': function(){
var playerId = this._id;
var selectedPlayer = Session.get('selectedPlayer');
if(selectedPlayer === playerId){
return 'selected';
}
},
'showSelectedPlayer': function(){
var selectedPlayer = Session.get('selectedPlayer');
return PlayersList.findOne(selectedPlayer);
}
});
Cloud9's editor uses ESLint, and using foo = 22 makes it think that there's a missing statement like var foo; somewhere. You can either choose to ignore this, or fix it as follows:
Add /*eslint-env meteor */ to the top so it doesn't give warnings about Meteor globals, and maybe you'll also need to add /* globals Player */ added too in case the error still stays. (I haven't tested this out, please let me know how it goes so I can improve the answer)
I solved the problem with a little workaround. I added coffeescript and then I used the # symbol on the global like you should, when you define a collection with coffeescript. And that was solving it for me. It works fine. As I opened the app in an new browser window, posts were available in the console.
Example:
#Posts = new Mongo.Collection('posts')

Meteor.js : How to run check() when arguments are Mongodb ObjectId's?

In some of my Meteor methods, I'm sending Mongodb ObjectId's from the client as arguments. I'd like to run these through Meteor's check() system but I can't seem to find anything that matches successfully with them.
I've tried
var someObjectId = Meteor.Collection.ObjectId();
check(someObjectId, Meteor.Collection.ObjectId()) // fails
check(someObjectId, { _str : String }) //fails
check(someObjectId, String) //fails
any help much appreciated !
Instead of:
check(someObjectId, Meteor.Collection.ObjectID());
Try without the parentheses:
check(someObjectId, Meteor.Collection.ObjectID);
Edit-
Note that the error message for this check isn't ideal.
check({}, Meteor.Collection.ObjectID);
// Error: Match error: Expected
You could assume the message should be something like
// Error: Match error: Expected ObjectId, got object
You can see why this happens in this snippet from the check package.
https://github.com/meteor/meteor/blob/devel/packages/check/match.js
if (pattern instanceof Function) {
if (value instanceof pattern)
return;
// XXX what if .name isn't defined
throw new Match.Error("Expected " + pattern.name);
}
Meteor.Collection.ObjectID does not have name property.
As an alternative solution, you could simply pass the hexadecimal string as an argument instead of the ObjectID.
var idValidator = Match.Where(function (id) {
check(id, String);
return /[0-9a-fA-F]{24}/.test(id);
});
check(new Meteor.Collection.ObjectID()._str, idValidator);
// success
check('', idValidator);
// Error: Match error: Failed Match.Where validation
check({}, idValidator);
// Error: Match error: Expected string, got object
check([], idValidator);
// Error: Match error: Expected string, got object <--- bug? I expect array
Note, this regular expression is pulled from here.
https://github.com/mongodb/js-bson/blob/master/lib/bson/objectid.js
You should use following to generate a random ObjectID:
var someObjectId = new Meteor.Collection.ObjectID();
As Cuberto said, you can then check it by Meteor.Collection.ObjectID:
check(someObjectId, Meteor.Collection.ObjectID)
Normally when using check() you're not in a position to generate a new Meteor _id. Here's an alternative using Match.check()
First extend the Match object with:
Match._id = Match.Where(function (id) {
check(id, String);
return /[a-zA-Z0-9]{17,17}/.test(id);
});
This is useful because you're likely to be checking _ids in many of your methods.
Now simply:
check(_id,Match._id);
more on this pattern
Complete answer to the original question:
First, define a matcher for a single object in your array of arguments:
Match._id = Match.Where(function (id) {
check(id, String);
return /[a-zA-Z0-9]{17,17}/.test(id);
});
Then you can call:
check(MyArrayOfArguments, [Match._id])