Select falsey documents in Meteor - mongodb

I have a Meteor collection where I want to specify a query to find all documents where an attribute is falsey. In other words, where it does not exist, null, false. In native Mongo the following syntax works:
find({category: "Cereal", showOnList: {"$ne": true}})
In this case, none of the documents have the element showOnList and therefore match my query. In Meteor (client side) I get the following error when using this syntax:
Uncaught SyntaxError: Unexpected token )
at Object.InjectedScript._evaluateOn (<anonymous>:904:140)
at Object.InjectedScript._evaluateAndWrap (<anonymous>:837:34)
at Object.InjectedScript.evaluate
(<anonymous>:693:21)InjectedScript._evaluateOn # VM220:904InjectedScript._evaluateAndWrap # VM220:837InjectedScript.evaluate # VM220:693
Anyone else run into this problem and have a workaround? Is this a Meteor bug?

You have a typo some where here is a fully working query I just tested
Posts.find({_id: {$ne: "M6RgPgC4KbnvLxz8W"}}).fetch()

I don't know how you could create this query with one statement. This is my workaround:
find({$or: [{ showOnList: null }, {showOnList: false}, {showOnList: {$exists: false}}] })

Related

MongoDB update deep nested field in the document

I try to update a field in the MongoDb like this
https://mongoplayground.net/p/_jFYJGi8z46
However i get this error
fail to run update: write exception: write errors: [The array filter for identifier 'e2' was not used in the update { $set: { sizes.$[e1].wares$[e2].reserved: "2" } }]
I can't find a bug :( ... it must be a very simple one
Right... one dot was missing :( This is solved
https://mongoplayground.net/p/obdFTr6G1kj

Mongodb Stitch realtime watch

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

Getting Mongo write error in Meteor

I am trying to insert a new field like this, and I am getting a write error.
If I remove this line, the program works fine.
UserDetails.update({userId: Meteor.UserId()}, {$inc: {score: 5}});
Error trace is:
I20141107-12:55:38.278(5.5)? Exception in Mongo write: TypeError: object is not a function
I20141107-12:55:38.323(5.5)? at packages/mongo/mongo_driver.js:293
I20141107-12:55:38.323(5.5)? at runWithEnvironment (packages/meteor/dynamics_nodejs.js:108)
I believe you just have a typo. It should be Meteor.userId()
Reference: https://docs.meteor.com/#/basic/Meteor-userId
UPDATE (11/9/14): It just dawned on me that you're using Meteor.userId() which gets the current user id from the Meteor.users collection. But it looks like you're trying to update the score for a user in a collection called UserDetails. The syntax to update a specific user id is this:
UserDetails.update("biwyMQCriR3KDFHod", {$inc: {score: 5} });
Where "biwyMQCriR3KDFHod" (with the double quotes) is the unique id value for that user.
I'm not sure how you're doing your update (perhaps you could share your code using http://meteorpad.com), but you might want to take a look at using Session.
Session References:
http://meteortips.com/book/sessions/
https://docs.meteor.com/#/basic/session

Mongodb - query with '$or' gives no results

There is extremely weird thing happening on my database. I have a query:
db.Tag.find({"word":"foo"})
this thing matches one object. it's nice.
Now, there's second query
db.Tag.find({$or: [{"word":"foo"}]})
and the second one does not give any results.
There's some kind of magic I obviously don't understand :( What is wrong in second query?
in theory, $or requires two or more parameters, so I can fake it with:
db.Tag.find({$or: [{"word":"foo"},{"word":"foo"}]})
but still, no results.
Your second query is perfectly fine, and it should work. Though the docs says, that $or performs logical operation on array of two or more expression, but it would work for single expression also.
Here's a sample that you can see, and try out, to get it to work: -
> db.col.insert({"foo": "Rohit"})
> db.col.insert({"foo": "Aman", "bar": "Rohit"})
>
> db.col.find({"foo": "Rohit"})
{ "_id" : ObjectId("50ed6bb1a401d9b4576417f7"), "foo" : "Rohit" }
> db.col.find({$or: [{"foo": "Rohit"}]})
{ "_id" : ObjectId("50ed6bb1a401d9b4576417f7"), "foo" : "Rohit" }
So, as you can see, both your query when used for my collection works fine. So, there is certainly something wrong somewhere else. Are you sure you have data in your collection?
Okaay, server admin installed mongodb from debian repo. Debian repo had 1.4.4 version of mongodb, aandd looks like $or is simply not yet supported out there :P

MongoDB Shell - access collection with period in name?

I have found a collection in one of our MongoDB databases with the name my.collection.
Is there a way to access this collection from the MongoDB shell, despite it having a point in the name?
> db.my.collection.findOne();
null
I'm pretty sure that that is not correct.
try this instead:
db["my.collection"].findOne();
you run into the same issue with hyphens or any other name that does not match on
[a-zA-Z_$][0-9a-zA-Z_$]
This limitation comes from valid named for javascript object properties.
if collection name is "my.collection"
db.my.collection.findOne(); // OK
null
if collection name is "my.1.collection"
db.my.1.collection.findOne(); // Not OK
SyntaxError: missing ; before statement
Fix:
db["my.1.collection"].findOne(); // Now is OK
null
Another foolproof method is:
db.getCollection("_SCHEMA").find()
While in the case of a underscore in the name, still cause a error with #Laura answer:
> db["_SCHEMA"].find()
2016-07-18T17:44:16.948+0200 E QUERY [thread1] TypeError: db._SCHEMA is undefined :
#(shell):1:1
Your code is correct. If it returns null it means that the collection is empty.