Type error on mongodb 3 - mongodb

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.

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.

Spring Data Embedded Mongo: 'unknown top level operator: $expr' on server

when I run any query containing $expr operation against Embedded Mongo I get the following error:
UncategorizedMongoDbException: Query failed with error code 2 and error message 'unknown top level operator: $expr' on server
The command runs fine against my local instance of mongo.
This is the version of embedded mongo I'm using: testCompile('de.flapdoodle.embed:de.flapdoodle.embed.mongo:2.1.1')
This is the query for reference:
Criteria.where("$expr").ne(Arrays.asList("$val.a", "$val.b"))
Found it.
flapdoodle was downloading a version of Mongodb that didn't have that feature by default.
You can override the default version by specifying the following in your
src/test/resources/application.properties
spring.mongodb.embedded.version=3.6.4
spring.mongodb.embedded.features=SYNC_DELAY,NO_HTTP_INTERFACE_ARG,ONLY_WITH_SSL

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">>}).

Updating mongodb references errors out due to field names

I have a MongoDB collection and I'm trying to update all the entries in it to change the name of a field used to store a reference. The Query I'm using is
db.products.find().forEeach(function(p) {
p.newField = p.oldField;
db.products.save(p);
});
The problem is that p.oldField is a DBRef following the standard format of { "$ref": "collection", "$id": ObjectId("...")}. When I try to run the db.products.save(p); Mongo returns the following error:
Sat Oct 1 13:00:57 uncaught exception: field names cannot start with $ [$db]
I'm using version 1.8.2 of the MongoDB shell. I have seen this work on an older version of the shell (1.6.5), which is where I originally came up with this query. But I can't seem to make this work on newer versions.