what is printjsononline in mongosh and how to replace it? - mongodb

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.

Related

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.

Mongo shell : can't update log level (setLogLevel is not a function)

I am trying to increase my mongo log level without success:
MongoDB shell version: 2.6.10
connecting to: test
> use TreeDB
switched to db TreeDB
> db.setLogLevel(5,"query")
2018-01-23T12:11:28.221+0100 TypeError: Property 'setLogLevel' of object TreeDB is not a function
How to correctly use this function?
> db.setLogLevel
TreeDB.setLogLevel
db.help() does not output this function.
docs.mongodb references it here since 3.0: https://docs.mongodb.com/manual/reference/method/db.setLogLevel/
I am using ubuntu 16.04, mongoshell 2.6.10, mongo: 3.6.2 (via docker)
As explained in the comments, I have to install at least 3.0 version of mongoshell.
This should be done following https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

How to read from a replicaset mongo by mongodb-erlang

1. {ok,P}= mongoc:connect({rs, <<"dev_mongodb">>, [ "dev_mongodb001:27017", "dev_mongodb002:27017"]}, [{name, mongopool}, {register, mongotopology}, { rp_mode, primary},{ rp_tags, [{tag,1}]}], [{login, <<"root">>}, {password, <<"mongoadmin">>}, {database, <<"admin">>}]).
2. {ok, Pool} = mc_topology:get_pool(P, []).
3. mongoc:find(Pool, {<<"DoctorLBS">>, <<"mongoMessage">>}, #{<<"type">> => <<"5">>}).
I used latest version in github, and got an error at step 3.
It seems my selector is not valid, is there any example of how to use mongodb-erlang ?
My mongodb version is 3.2.6, auth type is SCRAM-SHA1.
mongoc:find(Pool, <<"mongoMessage">>, #{<<"type">> => <<"5">>}).
I tried this in rs and single mode, still got this error.
Is there any other simple way to connect and read?
I just need to read some data once from mongo when my erlang program start, no other actions.
Todays version of mongo does not support tuple colldb due to new query api introduced in mongo 2.6
You should connect to DoctorLBS database instead, and than use
mongoc:find(Pool, <<"mongoMessage">>, #{<<"type">> => <<"5">>}).

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.

Mongo Shell No Method Find

I am running Debian with MongoDB shell version: 2.4.3
I run
use dbname
db.stats.find()
And it outputs the following
> db.stats.find()
Mon May 13 17:55:20.933 JavaScript execution failed: TypeError: Object function (scale){
return this.runCommand( { dbstats : 1 , scale : scale } );
} has no method 'find'
However running it on other collections works fine.
This mongo instance is being used with nodejs.
If you really created a collection named stats in your database dbname then I would advise you to rename it. In the shell the db object has a stats() method for looking at statistics of the database.
Meanwhile you can use slightly more complex syntax:
> db.getSiblingDB("dbname").getCollection("stats").find()
Fetched 0 record(s) in 4ms
Or if you are in dbname then:
> db.getCollection("stats").find()
I presume you want db.stats().