MongoDB unexpected token? when using $gte - mongodb

I'm trying to get all documents that are greater than the current date, based on an 'end_date' field.
I'm using this:
db.collection.find({end_date: {$gte: new Date()}})
But I get a syntax error near `{end_date:'
Syntax seems to right - am I missing something?
Thank you!

This syntax works just fine for me:
derick#whisky:/tmp$ mongo
MongoDB shell version: 2.0.3
connecting to: test
> use demo
switched to db demo
> db.collection.find({end_date: {$gte: new Date()}})
>

Related

what is printjsononline in mongosh and how to replace it?

In mongosh
% mongosh
Current Mongosh Log ID: 630639411fcf560da1e8d627
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.5.4
Using MongoDB: 6.0.1
Using Mongosh: 1.5.4
I am getting the error
test> printjsononline({teste : 1})
ReferenceError: printjsononline is not defined
But it used to work in mongo tool, before it was upgraded to mongosh. What did that function? How to replace it? This is used by an emacs mode (http://github.com/own-pt/sensetion.el) that I need to use.
Most functions which are removed in new mongosh are provided by the mongosh-snippets package in Github.
load('mongonative.js');
load('mongoassert.js');
load('mongotypes.js');
Or write your own function:
if (typeof tojsononeline == 'undefined') {
function tojsononeline(x) {
return EJSON.stringify(x)
.replace(/\{"\$date":"(.{19,23}Z)"\}/g, "ISODate(\"$1\")")
.replace(/\{"\$oid":"(\w{24})"\}/g, "ObjectId(\"$1\")")
.replace(/\{"\$numberDecimal":"(.+?)"\}/g, "Decimal128(\"$1\")");
}
}
As mentioned in the Mongosh documentation:
mongosh supports a subset of the mongo shell methods.
Achieving feature parity between mongosh and the mongo shell is an ongoing
effort.
I don't think the support for printjsononline is added yet. However, you can try printjson method of mongosh, if it works the same.
Check this list to find all the methods supported by mongosh.

Getting error for query in mongo 3.6.22 which works fine in 4.4.3 enterprise

I'm getting an error for mongo query in mongo version 3.6.22 error as follows
MongoError: arguments to $lookup must be strings, let: { profileIds:
"$ProfileIDs" } is type object
But this same query works fine in mongo version 4.4.3 enterprise, and the ProfileIDs are String
3.6 does not support let in $lookup. You need to write your query differently.

Type error on mongodb 3

When i try to insert data on mongodb 3 through command line it's showing following error
use video;
switched to db video
db.movies.insertOne({ "title": "Jaws", "year": 1975, "imdb": "tt0073195" });
2018-03-26T12:42:42.233+0530 E QUERY TypeError: Property 'insertOne' of object video.movies is not a function at (shell):1:11`
but video db also not created
Please help me to rectify this problem.
MongoDB supports db.collection.insertOne() from version 3.2, please check your mongodb version by using the mongo shell command
db.version()
References:
insertOne
version
Try with db.movies.insert instead of db.movies.insertOne and check If it's working fine. If it's working then your mongo version is less than 3.2. If not then share your mongoDb console Screenshot.

About mongodb 3.0.2 auth

I upgraded my mongodb server to version v3.0.2.
Everything seems to be working fine except for auth user creation, the documentation about this version states, that it works the same as the previous versions: http://docs.mongodb.org/manual/reference/method/db.createUser
But for some reason it doesn't seem to be working for me:
root#Bakalaurs:~# mongo
> use admin
switched to db admin
> db.addUser({user:"root", pwd:"asd", roles:[ "userAdminAnyDatabase", "readWrite" ] } )
2015-05-01T06:14:07.029-0400 E QUERY TypeError: Property 'addUser' of object admin is not a function
at (shell):1:4
> use bakalaurs
switched to db bakalaurs
> db.addUser({user:"bakalaurs", pwd:"asdf", roles:[ "readWrite" ]})
2015-05-01T06:15:36.595-0400 E QUERY TypeError: Property 'addUser' of object bakalaurs is not a function
at (shell):1:4
bye
Any ideas what am I doing wrong?
EDIT: nevermind, just noticed that now it's createUser instead of addUser, changed the function and it works fine now.
addUser has been deprecated since version 2.6:. You need use db.createUser() and db.updateUser() instead of db.addUser() to add users to MongoDB, see:
http://docs.mongodb.org/v3.0/reference/method/db.addUser/
The above link is redirected to db.createUser(). You can find more details regarding db.updateUser here:
http://docs.mongodb.org/v3.0/reference/method/db.updateUser/
db.addUser(<user document>)
Deprecated since version 2.6: Use db.createUser() and db.updateUser() instead of db.addUser() to add users to MongoDB.

mongodb error : invalid operator: $maxDistance

MongoDB shell version: 2.1.0
$near operator works fine. But as I try (in mongodb shell and nodejs):
db.locations.find({loc: {$near:[50,50],$maxDistance:50}})
error: { "$err" : "invalid operator: $maxDistance" }
I tried upgrading mongodb (if you know an easy way to do this I would really really thank you as I had a lot of trouble with trying to shut down the server in order to replace the bin folder...)
Thank you!
upgrading mongodb solved the problem. 10x!