load() function mongo shell for updatemany - mongodb

I am not getting output when i call update many via load(). My setVerboseShell(true)
However when i run the command explicitly in shell prompt it goes fine
MongoDB Enterprise > load('/opt/app/mongo/test.js') true
MongoDB Enterprise > setVerboseShell(true)
MongoDB Enterprise > load('/opt/app/mongo/test.js') true
MongoDB Enterprise > db.organizations.updateMany({"_id" : {$in : [ "120300007","160800001","175100009","193000031","201700008","235500001","237600001","24600001","272500006","278200081"]}},{$unset : {"asenodMigrationDetails.aseMigrationStatus" : ""}});
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 0 }

In interactive mode the mongo shell will display the results of the last command run.
In your example, the return value of load() is a boolean indicating the success of loading and evaluating JavaScript from the specified file.
If you want to output results from commands in test.js, you need to explicitly call print() or printjson().
Your test.js should instead look like:
var result = db.organizations.updateMany(
{"_id" : {
$in : ["120300007","160800001","175100009","193000031","201700008","235500001","237600001","24600001","272500006","278200081"]}},
{$unset : {"asenodMigrationDetails.aseMigrationStatus" : ""}}
)
printjson(result);
For more information see Write Scripts for the mongo shell in the MongoDB manual.

Related

mongo shell ReferenceError: database_name not found

Hello I've been using mongodb with mongo shell and I keep having the same problem.
While the database called Videos exists in MongoDB Compass and I can use it and add data to it, when I try to access it from the mongosh with this command:
Videos.getCollectionNames();
or with this command:
Videos.GBvideos.find();
I keep getting this error:
ReferenceError: Videos is not defined
This happens both with the integrated mongosh (inside the Compass env.) and with the mongosh executable from the installation path.
Any ideas how to get over this error?
I need to use the shell as what I want to do can't be done with the Compass
Compass:
You can do this way in single command:
mongos> db.getSiblingDB("Videos").getCollection("GBVideos").find()
{ "_id" : ObjectId("61eef2439f3fb15d1fef8877"), "test" : 1 }
{ "_id" : ObjectId("61eef24e9f3fb15d1fef8878"), "test" : 1 }
mongos>
Or if you need your way you must assingn to variable:
mongos> var Videos=db.getSiblingDB("Videos")
mongos> Videos.GBVideos.find()
{ "_id" : ObjectId("61eef2439f3fb15d1fef8877"), "test" : 1 }
{ "_id" : ObjectId("61eef24e9f3fb15d1fef8878"), "test" : 1 }
mongos>
use Videos switched to db Videos
is the way to change the database from the mongo shell kudos to R2D2!!

Compacting mongodb queries

With the mongo shell, I am doing something in a way which I think should be simpler. I make a first request to get a result needed for a second one.
Here is the first command:
MongoDB -prompt-:PRIMARY> db.Collection.find({"_p_unit":"TopCollection$npsV9rp1Gg","Order":2},{"_id":0,"AUDIO":1})
{ "AUDIO" : "audio_file_key_1a2b3c_Voice.bin" }
I use this result in the next command:
MongoDB -prompt-:PRIMARY> db.Collection.updateOne({"_p_unit":"TopCollection$BTDGm5LIcX","Order":2},{"$set":{ "AUDIO" : "audio_file_key_1a2b3c_Voice.bin" }})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
MongoDB -prompt-:PRIMARY>
Now this is my question:
Is there a way to group the two commands above into one?
Though I tried a few ideas coming up my mind, nothing worked.

Using upsert with the updateOne() method to perform an update operation

I’m using upsert with the updateOne() method as bellow to perform an update.
db.practice.updateOne(
{“title”:“Night Life”},
{$set: detail},
{upsert: true}
)
My query returns the following:
{
"acknowledged" : true,
"matchedCount" : 0,
"modifiedCount" : 0,
"upsertedId" : ObjectId("5f8884fed29ded706c3c6737")
}
Below is the detail variable:
let detail = {
“title” : “Night Life”,
“year” : 2021,
“rated” : “PG-13”,
“released” : 2021,
“runtime” : 60,
“countries” : [
“USA”,
“UK”
],
“genres” : [
“comedy”,
“drama”
],
“director” : “Alpha Ly”,
“actors” : [
“Alpha Ly”,
“Kris Dasha”,
“Hope Grace”
]
}
When I check my collection in Compass, I don’t see the document with the ObjectId("5f8884fed29ded706c3c6737
However, when I run the following command db.practice.find({"director": "Alpha Ly"}).pretty(),
it returns the entry I've looking for in Compass.
NB: the number of documents in my collection is still the same as created.
What seems to be the problem here. Why Compass is not displaying the entry?
It seems like I was not using the correct database.
In order to execute the command, I was supposed to use the use command followed by the name of my database to switch to my database. Since I didn't, the update query created another collection and inserted the document there. Therefore, I couldn't find it in my database.
Thanks

Birt report how to write mongodb db command

I want to import report query from js file.
To do that I am choosing Run Database Command command type. But I dont know how to write the query.
I tried sth like this db.user.find({}) blindly but that does not work.
Any doc/resource on using this feature?
If you select Run Database Command you enter a valid JSON-Object in the Expression. This Object will be executed in a call to db.runCommand(), a MongoDB interface to execute Non-CRUD operations.
Enter { isMaster: 1 } and you will see the Result
{
"ismaster" : true,
"maxBsonObjectSize" : 16777216,
"maxMessageSizeBytes" : 48000000,
"maxWriteBatchSize" : 1000,
"localTime" : ISODate("2016-04-26T16:24:18.317Z"),
"maxWireVersion" : 3,
"minWireVersion" : 0,
"ok" : 1
}
Update to read the intention of what you want to achieve:
Select the empty line from Command Type and enter the name of the collection you want to use in the line above. Click on $query Expression... and add the JSON-Object to restrict your query. Then select from the displayed attributes the one you want to add to your result set.

MongoDb: how to write your mongoDb query result in a file?

While running a query i want a result of that query into another file
My query is running successfully in mongo terminal.
db.questions.find({"question" : {$regex : ".*.*"}},{question :1,_id:0,id:1});
{ "id" : 0, "question" : "Amount was debited from my account, but ticket was not
generated. What should I do now?" }
{ "id" : 1, "question" : "How safe is goCash?" }
{ "id" : 2, "question" : "How referral program and goCash works?" }
Now check following image:
here
By this i can write in any type of file by just adding >> filename.extension
Now the main problem is with this one:
When i use find instead of findOne and use a regular expression, it shows unexpected token
Anyone knows how i can modify it to get the desired result.
You are using "(qoutes) inside the command line. Try using {'question' : { \$regex : '.*'}}.
Further the db.collection.find() method returns a cursor. Redirecting it to a file will just write the cursor json.To access the documents, you need to iterate the cursor.
mongo localhost/database -eval "var cursor = {'question' : { \$regex : '.*'}}; while(cursor.hasNext()){ printjson( cursor.next())}"
All the examples in the blog return result as json format.
You can also use any of the mongo-drivers to write your result to a file.