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.
Related
I'm trying to insert one registry in mongodb with mongosh and ubuntu bash. I've get one registry with mongosh . I have to edit 3 fields and make an insert. I thought to make the edition with jq but I don't get it done.
{ "_id": {"fileName": "xxxxxx","namespace": "yyyyyy" },
"metainfo": {"file-type": "csv","environment": "int",
"creation-date": 1672306975130000000,"file-name":"xxxxxxx" }
}
I've to edit creation-date (is the date en nanos), the enviroment, change part of the fileName (make a substring). I've get the document with --eval "EJSON.stringlify(....)"
the command with jq I've tried is:
newDocument=$(echo "$fileData" | jq '.metainfo.environment |= "pro"')
and gives me error:
parse error: Invalid numeric literal at line 1, column 8
I've validated the JSON and it's well formed.
After made the changes I've to make Then make the insert. I've thought made with
--eval "......insertOne(EJSON.stringlify($newDocument))"
is this correct? What would be the best mannerto do all this?
Thanks for all.
The error was giving me because I was making the request without --quiet parameter.
The mongo shell allows json without problems.
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.
I get the following error (dump MongoDB 3.2) (restore MongoDB 3.4):
Failed: ngivr-dev.ledgerhelpers: error creating indexes for ngivr-dev.ledgerhelpers: **createIndex error:** **The field 'safe' is not valid for an index specification.** Specification: **{ unique: true, name: "ledgerId_1", safe: null, ns: "ngivr-dev.ledgerhelpers", background: true, key: { ledgerId: 1 } }**
Looks like the safe index is null. But how can i use it with MongoDB 3.4? 3.2 is ok.
safe=true is not an index specification.
In previous versions of MongoDB, lower than 3.4, extra indexes specifications can be added. Those were used by specific drivers.
In 3.4, mongodb added a validation on indexes specification:
Ensuring that the specified index options are valid. Previous
versions ignored invalid options.
That's why you have this error.
I am afraid you need to ensure that the index in your 3.2 version does not have invalid index specificaitons, and after that do the mongodump.
As kz_sergey says in his answer, you can mongorestore using --noIndexRestore, that should work fine.
Why do you restore indexes? --noIndexRestore and create them again.
In the spirit of Aymeric's comment, you can use this awk one-liner to replace the "safe" property in your .metadata.json files.
awk -i inplace '{gsub(",\"safe\":null", ""); print}' *.metadata.json
Run it in the directory of your MongoDB export. This approach allows you to keep the indexes, but drop the "safe" option.
find . -type f -name "*.metadata.json" -exec sed -i 's/,"safe":null//g' {} \;
This works and you will keep your indexes! It find all files in the present location (.) then using the same process (exec) replace in file (sed -i) according to the following regex which is basically saying all occurrences of "safe":null with nothing.
Replace the "." argument with the path to the directory where your mongodb exports are stored.
Since I wanted to keep all indexes, and none of the methods above worked in my case, I've just edited all *.metadata.json files and manually removed all occurrences of "safe":true.
Context: I'm using an old database which I cannot upgrade to 4.x or above - it was originally running on 3.2, but there's no version of mongo running on new Macs with Apple Silicon, so I had to use 3.4 (the first version available) and do a dump from the old Mac and a restore from the new Mac.
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
I need to create database named "my-database" (yes, with "-" or "." sym). I tried default syntax to achive that (http://docs.mongodb.org/manual/tutorial/getting-started/):
1) use my-database
2) db['test'].insert( {'foo':'bar'} )
3) show dbs // oh, God! I have done it!
But after first line I get
Error: Line 1: Unexpected identifier
How can I create database with name "my.db" / "my-db" via mongo shell?
UPDATE: this error occurs if I try to use commands from list above in Robomongo console... In mongo shell all works well.
> use test-db
switched to db test-db
> db['test-collection'].insert( {'foo':'bar'} );
> db['test-collection'].find();
{ "_id" : ObjectId("53f36049812d284697e80ffd"), "foo" : "bar" }
It's because Robomongo doesn't use the same V8 engine that mongo uses - it uses Spidermonkey. (thanks Lix)