Rename MongoDB 4.0.4 Database Name - mongodb

Getting below error :
db.copyDatabase("old_db","new_db","localhost:27017");
WARNING: db.copyDatabase is deprecated. See http://dochub.mongodb.org/core/copydb-clone-deprecation
{
"note" : "Support for the copydb command has been deprecated. See http://dochub.mongodb.org/core/copydb-clone-deprecation",
"ok" : 1
}
https://docs.mongodb.com/manual/release-notes/4.0-compatibility/#copydb-and-clone-commands
I went to this link, but there's no solution regarding this.
Any leads would be appreciated.

Use mongodump and mongorestore or write a script using the drivers.

Related

mongoimport Error, Failed: invalid JSON input

I have many mongodb instance files like below;
[
{
"_id" : ObjectId("35d455de983c0e6a53ea0848"),
"createdAt" : ISODate("2019-12-05T23:25:04.347+0000"),
"__v" : NumberInt(0)
},
{
"_id" : ObjectId("1ecbe0f75df8ccd52a7b1662"),
"createdAt" : ISODate("2019-12-17T12:40:53.521+0000"),
"__v" : NumberInt(0)
}
]
I couldn't import these files because of invalid format.
mongoimport --db DATABASENAME --collection COLLECTIONNAME --file filename.json --jsonArray
And it says;
Failed: invalid JSON input. Position: 16. Character: O
Is there any other way to import those files?
If not, how can I convert them to be imported?
you can use --legacy option of mongoimport in order to import the file with the format you want with ObjectId("an oid"), NumberInt(an int) or new Date("an iso date" or any mongo extension available in extended json v1 : https://docs.mongodb.com/manual/reference/mongodb-extended-json-v1/)
Solution 1 (semi-auto):
We can import database only in json/csv format so remove all invalid characters such as ObjectId, ISODate, NumberInt, ()...
Solution2 (auto):
Finally, I found an alternative solution.
We can't import such data even in Mongo Compass.
But fortunately, we can import/export database in various formats in Studio 3T for MongoDB.
I think Studio 3T is better than Mongo Compass, but it is not free.
After 30 days, we can't use Studio 3T trial version.
I will be very thankful if you let me know a way to reset Studio 3T.
invalid JSON input. Position: 96. Character: N
For me it was "N" for NaN that I replaced with 0 and it wroked.
It also worked with adding the --legacy option, but I preferred to fix the file as the legacy option did not work within my docker container.
I had same problem, the error was about a property called '$init' that was added by mongoose automatically.
I removed all those by Sublime and imported again by 3T.

mongodb text search: invalid operator: $search

Closely related to this, but I'm using mongod version 3.2 which means its a different problem.
I created a text index:
> db.mycollection.createIndex({body:'text'})
Then I try to search it:
> db.mycollection.find({$text:{$search:'foo'}})
Error: error: { "$err" : "invalid operator: $search", "code" : 10068 }
It should work. (changes to the $text operator for v3.2 shouldn't affect this.) What am I doing wrong?
The problem was caused by an old database (created by mongod 2.4) that had survived the mongodb uninstall / reinstall process. The official doumentation didn't mention it, but while uninstalling the old version, it was necessary to do:
sudo rm -r /data/db/*
(Perhaps because of how 2.4 was installed?) Deleting these files, and bringing my old data forward using the mongodump and mongorestore commands worked.

How to set rs.slaveOk() in secondary mongodb servers in replicaset via commandline?

How to set rs.slaveOk() in secondary mongodb servers in replicaset via commandline?
I tried following methods :
${MONGO_HOME}/bin/mongo --port ${MONGO_PORT2} --host ${MONGO_SECONDARY2} --eval "printjson(rs.slaveOk())"
${MONGO_HOME}/bin/mongo --port ${MONGO_PORT2} --host ${MONGO_SECONDARY2} --eval "printjson(rs.slaveOk(true))"
${MONGO_HOME}/bin/mongo --port ${MONGO_PORT2} --host ${MONGO_SECONDARY2} --eval "printjson(db.getSiblingDB('admin').getMongo().setSlaveOk())"
the command executes with undefined in the output log.
I am trying to set this via the shell in primary server.
Create a file /etc/mongorc.js and add rs.slaveOk() there. The file is being evaluated on each shell startup.
For more information have a look here
From MongoDB version 4.4 onwards, you might get a warning displayed like:
WARNING: slaveOk() is deprecated and may be removed in the next major release. Please use secondaryOk() instead.
So, please prefer using rs.secondaryOk()
Calling the below should work fine, there is no return type for the method so nothing will get printed back to the screen
${MONGO_HOME}/bin/mongo --port ${MONGO_PORT2} --host ${MONGO_SECONDARY2} --eval "rs.slaveOk()"
Running rs.slaveOk in the mongo.exe will also how how it is implemented as it is just a helper method:
> rs.slaveOk
function (value) { return db.getMongo().setSlaveOk(value); }
>
And also the setSlaveOk method:
> db.getMongo().setSlaveOk
function ( value ) {
if( value == undefined ) value = true;
this.slaveOk = value;
}
You could always try to query one of the collections on the secondary to make sure the node is queryable:
> db.test.findOne()
null
Update - bit more clarity
Setting slaveOk() is only valid for that console session that it was executed in, so you would need to pass in a script or stay connected to the console with the --shell arguments for exmaple
C:\mongodb\bin>mongo.exe --port 27012 --eval "rs.slaveOk()" --shell
MongoDB shell version: 3.0.5
connecting to: 127.0.0.1:27012/test
type "help" for help
rs1:SECONDARY> db.test.find()
{ "_id" : ObjectId("5630fdf2af4abd9f8ae7f79c"), "test" : true }
rs1:SECONDARY>
If we don't pass in the rs.slaveOk() the we get the following response:
C:\mongodb\bin>mongo.exe --port 27012 --shell
MongoDB shell version: 3.0.5
connecting to: 127.0.0.1:27012/test
type "help" for help
rs1:SECONDARY> db.test.find()
Error: error: { "$err" : "not master and slaveOk=false", "code" : 13435 }
rs1:SECONDARY> exit
bye
JFYI :
looks like rs.slaveOk() will be deprecated soon, instead MongoDB suggest to use rs.secondaryOk()
Following is the official warning you gonna see in MongoShell:
WARNING: slaveOk() is deprecated and may be removed in the next major
release. Please use secondaryOk() instead.
Cheers

How to execute the mongo cloneCollection command to copy a collection to a remote server

I'd like to copy a collection from one database to an instance on another server. From other stackoverflow questions, I understand the correct way to do that is with this command:
{ cloneCollection: "<collection>", from: "<hostname>", query: { <query> } }
via http://docs.mongodb.org/manual/reference/command/cloneCollection/
However, I don't understand where do I enter this command? It isn't accepted as...
$ mongod { cloneCollection: "remote", from: "ec2-whatever-amazon.com"}
How do I copy a remote collection at db.remote.collname to db.local.collname using this cloneCollection syntax via command line?
MongoDB database commands are run using db.runCommand() from the mongo shell. Refer to http://docs.mongodb.org/manual/tutorial/use-database-commands/.
Try something like this (using another database command for simplicity):
$ mongo
> db.runCommand({ isMaster: 1})
{
"ismaster" : true,
"maxBsonObjectSize" : 16777216,
"localTime" : ISODate("2014-02-18T22:30:04.417Z"),
"ok" : 1
}
>

What mongodb command I should run to know server info?

For example, I want to know the database directory that is used by mongodb to run what it is?
The documentation said that the default data is in /data/db however, there is no such directory
I wonder if there is a mongodb command to get that simple info. I look through the web and could not find it.
You can see the Configuration Parameters used with:
> db.serverCmdLineOpts()
For example:
{
"argv" : [
"mongod",
"--dbpath",
"/usr/local/db"
],
"parsed" : {
"dbpath" : "/usr/local/db"
},
"ok" : 1
}
Any parameters not specifically listed will be using their default values.
Just create a folder called data anywhere in your system.
Then let Mongo know where the this folder is by updating the path.
in windows you would run this on the command line.
C:\mongodb\bin\mongod.exe --dbpath c:\test\mongodb\data
This is where your mongo stores your data.