MongoDB :error doing update (anon):1552 on Update command - mongodb

I am using MongoDB Version of 1.2.6
I was doing a multiple update on a collection ( If a field named yesterday exists ,update it or else create field called yesterday for that record )
This is my query
db.userLoginCount.update({yesterday : {$exists : false}}, {$set: {yesterday : '07082013'}},false,true)
Once i run this , i am getting an error as
Tue Jul 9 02:43:05 Error: error doing update (anon):1552
Please tell me what is wring with the above query ??

Related

How to insert record to mongoDB6 collection using PutMongo processor in Apache Nifi?

When I am trying to insert the record(json) to mongodb collection. I am getting exception
{ "ok" : 0.0, "errmsg" : "Unsupported OP_QUERY command: insert. The client driver may require an upgrade. For more details see https://dochub.mongodb.org/core/legacy-opcode-removal", "code" : 352, "codeName" : "UnsupportedOpQueryCommand" }
I have MongoDb 6.0 version configured
MongoDb removes Opal_insert operation in new version.How to resolve it ?

mongo shell command not accept use db command

i am trying to pull records form mongo data base with json format to do that i am running below js :
conatin of get_DRMPhysicalresources_Data.js
cursor = db.physicalresources.find().limit(10)
while ( cursor.hasNext() ){
print( JSON.stringify(cursor.next()) );
}
the command i run to get the records :
mongo host_name:port/data_base_name -u user -p 'password' --eval "load(\"get_DRMPhysicalresources_Data.js\")" > DRMPhysicalresources.json
and i am able to get the result as josn formate inside DRMPhysicalresources.json , now i want to switch to other data base using use command i try add use db as below :
conatin of get_DRMPhysicalresources_Data.js
use db2_test
cursor = db.physicalresources.find().limit(10)
while ( cursor.hasNext() ){
print( JSON.stringify(cursor.next()) );
}
the command i run to get the records :
mongo host_name:port/data_base_name -u user -p 'password' --eval "load(\"get_DRMPhysicalresources_Data.js\")" > DRMPhysicalresources.json
but i am getting below errors :
MongoDB shell version v4.2.3
connecting to: "some data base info"
Implicit session: session { "id" : UUID("8c85c6af-ebed-416d-9ab8-d6739a4230cb") }
MongoDB server version: 4.4.11
WARNING: shell and server versions do not match
2022-04-11T13:39:30.121+0300 E QUERY [js] uncaught exception: SyntaxError: unexpected token: identifier :
#(shell eval):1:1
2022-04-11T13:39:30.122+0300 E QUERY [js] Error: error loading js file: get_DRMPhysicalresources_Data.js :
#(shell eval):1:1
2022-04-11T13:39:30.122+0300 E - [main] exiting with code -4
is there are any way to add use db2_test without break it ?
You can try this:
db = new Mongo().getDB("myOtherDatabase");
or
db = db.getSiblingDB('myOtherDatabase')
( this is the exact js equivalent to the 'use database' helper)
docs

MongoDB does not remove documents from a collection with a configured TTL Index

I try to get started with TTL indices in mongo, and wanted to get a simple demo setup running, but I just cannot get it to work and I am not sure what I do wrong.
Mongod version:
$ mongod --version
db version v4.4.5
Build Info: {
"version": "4.4.5",
"gitVersion": "ff5cb77101b052fa02da43b8538093486cf9b3f7",
"openSSLVersion": "OpenSSL 1.1.1m 14 Dec 2021",
"modules": [],
"allocator": "tcmalloc",
"environment": {
"distmod": "debian10",
"distarch": "x86_64",
"target_arch": "x86_64"
}
}
I start with a completely fresh, default configured mongod instance:
$ mkdir /tmp/db && mongod --dbpath /tmp/db
Then I run the following commands
use test
db.ttldemo.insertOne({
created_at: Date(Date.now() + 60_000)
})
db.ttldemo.createIndex({created_at: 1}, {expireAfterSeconds: 1})
db.ttldemo.find()
I would expect the document to vanish after some time, but even after running find() after waiting for a few minutes, the document is still present.
Is there anything I am missing here?
db.adminCommand({getParameter:1, ttlMonitorSleepSecs: 1}) yields 60 to me
I tried in mongo 4.2.17, your insertion of documents yields a document like:
{
"_id" : ObjectId("61f17ea34844b5f0505d80ea"),
"created_at" : "Wed Jan 26 2022 19:02:27 GMT+0200 (GTB Standard Time)"
}
when the
db.getCollection('ttldemo').insertOne({created_at: new Date()})
yields a document like:
{
"_id" : ObjectId("61f17f404844b5f0505d80ec"),
"created_at" : ISODate("2022-01-26T17:05:04.023Z")
}
Emphasis here on the created_at field, where this on the first case is a string, in the second case is an ISODate.
as from the documentation , this index works on dates only:
To create a TTL index, use the createIndex() method on a field whose value is either a date or an array that contains date values, and specify the expireAfterSeconds option with the desired TTL value in seconds.
The solution here is to construct the date as follows:
db.getCollection('ttldemo').insertOne({created_at: new Date(new Date().getTime()+60000)})
this will create a new ISODate based on the current date + 60 seconds.

mongo db error - SyntaxError: missing ; before statement

when i try to run this command on cmd.
mongo test --eval 'db.testcollection.find({ createdate: {$gte: ISODate("2020-07-28 10:08:37.579Z"),$lt: ISODate("2020-08-04 13:42:40.975Z")}}, {place:1, _id:0}).forEach(function(x) { print(x.place); })'
it throws this error
MongoDB server version: 4.0.5
2020-08-06T13:33:49.336+0530 E QUERY [js] SyntaxError: missing ; before statement #(shell eval):1:19
i try to add a semicolon at the end. it still throws same error.
mongo test --eval 'db.testcollection.find({ createdate: {$gte: ISODate("2020-07-28 10:08:37.579Z"),$lt: ISODate("2020-08-04 13:42:40.975Z")}}, {place:1, _id:0}).forEach(function(x) { print(x.place); });'
Error is at line 1, column 19. So placing ; at the end will not solve the issue.
Could you use
mongo --eval 'db.testcollection.find({ createdate: {$gte: ISODate("2020-07-28 10:08:37.579Z"),$lt: ISODate("2020-08-04 13:42:40.975Z")}}, {place:1, _id:0}).forEach(function(x) { print(x.place); })' test
test is the db name where testcollection is present.

Append key value if mongodb collection does not have the value?

I have a db with a bunch of collections.
Some of them have 'status' and some don't.
How can I insert 'status':'pending' into the collections that lack 'status' - but not overwrite the collections that already have a status?
Using pymongo / flask / python 2.7
I tried this:
orders = monDB.find('order')
for order in orders:
if not order['status']:
monDB.update('order', {'status':'pending'})
print 'success'
But nothing happens. What am I doing wrong?
Use $exists to check if the field exists and $set to create it if it doesn't. Assuming monDB is your collection:
monDB.update({'status': {'$exists' : False}}, {'$set' : {'status': 'pending'}}, multi=True)
In the mongo shell:
> db.myQueue.update({status: {$exists: false}}, {$set : {status: 'pending'}}, false, true)
See http://docs.mongodb.org/manual/reference/operators/ and http://docs.mongodb.org/manual/applications/update/#crud-update-update.