Just fetch a single attrbute from a single document - mongodb

I need to pull the latest date from a collection on mongo db and set it in a shell script.
LASTDOCDATE=mongo mongo.com:27017/tracking -u user -p pw --authenticationDatabase authdb --eval 'db.TRACKING_DATA.find({},{datecreated :1}).limit(1).sort({datecreated:-1}).map(function (doc){ return doc.datecreated; })'
echo $LASTDOCDATE
This to be set but when run through the terminal produces:
connecting to: mongo.com:27017/tracking
Mon Jul 27 2015 16:28:08 GMT-0700 (PDT)
have can I pull just the date attribute to be set in a shell script as a variable

Wrap your call with the printjson() method of the shell in order to get an output string:
LASTDOCDATE=mongo mongo.com:27017/tracking -u user -p pw \\
--authenticationDatabase authdb \\
--eval 'printjson(db.TRACKING_DATA.find({},{datecreated :1}).limit(1).sort({datecreated:-1}).map(function (doc){ return doc.datecreated; }))'
Or just print, while referencing the single element:
LASTDOCDATE=mongo mongo.com:27017/tracking -u user -p pw \\
--authenticationDatabase authdb \\
--eval 'print(db.TRACKING_DATA.find({},{datecreated :1}).limit(1).sort({datecreated:-1}).toArray()[0].datecreated'
Notating the single array element, and then the property:
.find({},{datecreated :1}).limit(1).sort({datecreated:-1}).toArray()[0].datecreated'
Or findOne() like this with $orderby:
.findOne(
{ "query": {}, "$orderby": { "datecreated": 1 }},
{ "_id": 0, "datecreated": 1 }
).datecreated
So .print() or .printjson() depending on the output format you want. Or even .valueOf() on the "datecreated" to just get the timestamp value rather than the string.

Related

Run a command in mongodb container with credentials from outside

I want to set the compatibility version in mongodb, which is running in a container, but from outside:
docker exec -it docker-compose_mongodb_1 bash -c 'mongo -uroot -p rootpassword --eval "db.adminCommand( { setFeatureCompatibilityVersion: "4.0" } )"'
MongoDB shell version v4.0.23
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("7945d913-f77c-4242-be33-af8e20c07374") }
MongoDB server version: 4.0.23
{
"ok" : 0,
"errmsg" : "Command argument must be of type String, but was of type double in: { setFeatureCompatibilityVersion: 4.0, lsid: { id: UUID(\"7945d913-f77c-4242-be33-af8e20c07374\") }, $db: \"admin\" }. See http://dochub.mongodb.org/core/4.0-feature-compatibility.",
"code" : 14,
"codeName" : "TypeMismatch"
}
Any idea what I am doing wrong?
It looks like a quoting problem: you seem to have double quotes around "4.0", but you've already quoted the entire argument to the --eval argument, so the effect of these quotes isn't what you want (the value (4.0) ends up unquoted, hence the error ("argument must be of type String, but was of type double").
In theory you can just escape the inner quotes:
docker exec -it docker-compose_mongodb_1 bash -c 'mongo -uroot -p rootpassword --eval "db.adminCommand( {setFeatureCompatibilityVersion: \"4.0\" } )"'

MongoDB: FailedToParse: Bad characters in value

mongodump command:
mongodump --host myhost.com --port 12345 --username myUsername --password PSWRD --out /opt/somepath --db myDb --collection my_collection --query "{ content_type_id: { \$not: { \$eq: db.my_type.findOne({slug: 'form_submissions'} )._id } } }" --verbose
Results in:
assertion: 16619 code FailedToParse: FailedToParse: Bad characters in value:
offset:33 of:{ content_type_id: { $not: { $eq: db.my_type.findOne({slug: 'form_submissions'} )._id } } }
That's not a valid query. --query must be a JSON document. Your error is in thinking that mongodump is something programmatic like the mongo shell that can evaluate the findOne and substitute the value into the query. This is not the case. You can find the _id from the result of the findOne and put it in the mongodump --query manually. Use extended JSON format for an ObjectId type, if that is the type of _id.

How to use mongoexport with query script file

I'm trying to follow this tutorial: http://www.ultrabug.fr/tag/mongoexport/
and use a sh file for the query line.
this is my file:
#!/bin/bash
d=`date --date="-3 month"`
echo "{ timeCreated: { "\$lte": $d} }"
this is my mongoexport line:
mongoexport --db game_server --collection GameHistory -query /home/dev/test2.sh --out /home/dev/file.json
I keep getting:
assertion: 16619 code FailedToParse: FailedToParse: Expecting '{': offset:0 of:/home/dev/test2.sh
why? How can I make this work?
I found several errors in your approach, let's examine them one by one.
Date format
MongoDB expects date to be a number or ISO 8601 string.
Unfortunately, unix date utility have no build-in support for this format, so you should use:
d=`date --date="-3 month" -u +"%Y-%m-%dT%H:%M:%SZ"`
Using extended JSON
JSON specification have no support for dates, so you should use MongoDB extended JSON. So, your final query should look like this:
{ "timeCreated": { "$lte": { "$date": "2014-05-12T08:53:29Z" } } }
test.sh output
You messed up with quotation marks. Here is a script example, outputting correct JSON:
#!/bin/bash
d=`date --date="-3 month" -u +"%Y-%m-%dT%H:%M:%SZ"`
echo '{ "timeCreated": { "$lte": { "$date": "'$d'" } } }'
Passing query to mongoexport
mongoexport expects --query to be a JSON string, not .sh script. So, when you're passing file path to --query, mongoexport expects it to be a JSON file.
To fix it you should execute test2.sh yourself and pass resulting string to mongoexport:
mongoexport --db game_server --collection GameHistory \
--query "`./test2.sh`" --out ./test2.json
N.B. Notice " quotation marks around ./test2.sh call. They're telling bash to treat ./test2.sh output as a single parameter, ignoring all inner quotation marks and whitespaces.
You need to add back ticks around a script or command to have it evaluated:
mongoexport --db game_server --collection GameHistory \
-query `/home/dev/test2.sh` --out /home/dev/file.json

Insert json file into mongodb

I am new to MongoDB. After installing MongoDB in Windows I am trying to insert a simple json file using the following command:
C:\>mongodb\bin\mongoimport --db test --collection docs < example2.json
I am getting the following error:
connected to: 127.0.0.1
Fri Oct 18 09:05:43.749 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Field name expected: offset:43
Fri Oct 18 09:05:43.750
Fri Oct 18 09:05:43.750 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Expecting '{': offset:0
Fri Oct 18 09:05:43.751
Fri Oct 18 09:05:43.751 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Field name expected: offset:42
Fri Oct 18 09:05:43.751
Fri Oct 18 09:05:43.751 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Expecting '{': offset:0
Fri Oct 18 09:05:43.751
Fri Oct 18 09:05:43.752 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Field name expected: offset:44
Fri Oct 18 09:05:43.752
Fri Oct 18 09:05:43.752 exception:BSON representation of supplied JSON is too large: code FailedToParse: FailedToParse: Expecting '{': offset:0
Fri Oct 18 09:05:43.752
Fri Oct 18 09:05:43.752 check 0 0
Fri Oct 18 09:05:43.752 imported 0 objects
Fri Oct 18 09:05:43.752 ERROR: encountered 6 error(s)s
example2.json
{"FirstName": "Bruce", "LastName": "Wayne",
"Email": "bwayne#Wayneenterprises.com"}
{"FirstName": "Lucius", "LastName": "Fox",
"Email": "lfox#Wayneenterprises.com"}
{"FirstName": "Dick", "LastName": "Grayson",
"Email": "dgrayson#Wayneenterprises.com"}
What do I need to do to import new json file into mongodb?
Use
mongoimport --jsonArray --db test --collection docs --file example2.json
Its probably messing up because of the newline characters.
Below command worked for me
mongoimport --db test --collection docs --file example2.json
when i removed the extra newline character before Email attribute in each of the documents.
example2.json
{"FirstName": "Bruce", "LastName": "Wayne", "Email": "bwayne#Wayneenterprises.com"}
{"FirstName": "Lucius", "LastName": "Fox", "Email": "lfox#Wayneenterprises.com"}
{"FirstName": "Dick", "LastName": "Grayson", "Email": "dgrayson#Wayneenterprises.com"}
This worked for me - ( from mongo shell )
var file = cat('./new.json'); # file name
use testdb # db name
var o = JSON.parse(file); # convert string to JSON
db.forms.insert(o) # collection name
Use below command while importing JSON file
C:\>mongodb\bin\mongoimport --jsonArray -d test -c docs --file example2.json
the following two ways work well:
C:\>mongodb\bin\mongoimport --jsonArray -d test -c docs --file example2.json
C:\>mongodb\bin\mongoimport --jsonArray -d test -c docs < example2.json
if the collections are under a specific user, you can use -u -p --authenticationDatabase
This solution is applicable for Windows machine.
MongoDB needs data directory to store data in. Default path is C:\data\db. In case you don't have the data directory, create one in your C: drive, unless different VolumeName is used e.g. H: (or any other relevant VolumeName) which is the root of your machine;
Place the .json file you want to import within: C:\data\db\ .
Before running the command copy-paste mongoimport.exe from C:\Program Files\MongoDB\Tools\100\bin (default path for mongoimport.exe) to the directory of the C:\Program Files\MongoDB\Server\[your_server_version]\bin
Open the command prompt from within C:\data\db\ and type the following command by supporting the specific databasName, collectionName and fileName.json you wish to import :
mongoimport --db databaseName --collection collectionName --file fileName.json --type json --batchSize 1
Hereby,
batchSize can be any integer as per your wish
mongoimport --jsonArray -d DatabaseN -c collectionName /filePath/filename.json
Open command prompt separately
and check:
C:\mongodb\bin\mongoimport --db db_name --collection collection_name< filename.json
In MS Windows, the mongoimport command has to be run in a normal Windows command prompt, not from the mongodb command prompt.
It happened to me couple of weeks back. The version of mongoimport was too old. Once i Updated to latest version it ran successfully and imported all documents.
Reference: http://docs.mongodb.org/master/tutorial/install-mongodb-on-ubuntu/?_ga=1.11365492.1588529687.1434379875
In MongoDB To insert Json array data from file(from particular location from a system / pc) using mongo shell command. While executing below command, command should be in single line.
var file = cat('I:/data/db/card_type_authorization.json'); var o = JSON.parse(file); db.CARD_TYPE_AUTHORIZATION.insert(o);
JSON File: card_type_authorization.json
[{
"code": "visa",
"position": 1,
"description": "Visa",
"isVertualCard": false,
"comments": ""
},{
"code": "mastercard",
"position": 2,
"description": "Mastercard",
"isVertualCard": false,
"comments": ""
}]
It works with JS and Node
Preconditions:
Node
Mongo - either local installed or via Atlas
server.js:
var MongoClient = require('mongodb').MongoClient;
var fs = require('fs')
export function insert(coll) {
MongoClient.connect('uri', (err, db) => {
var myobj = fs.readFileSync("shop.json").toString()
myobj = JSON.parse(myobj)
db.db(dbWeb).collection(coll).insertMany(myobj, (err, res) => {
db.close();
});
});
}
shop.json:
[
{
"doc": "jacke_bb",
"link": "http://ebay.us/NDMJn9?cmpnId=5338273189",
},
{
"doc": "schals",
"link": "https://www.ebay-kleinanzeigen.de/s-anzeige/4-leichte-schals-fuer-den-sommer/2082511689-156-7597",
}
]
As one see, the json starts with [ and ends with ] and the insertMany is used. This leads to a correct nested insertion of the array into the collection.

Mongo query works in mongo shell but not in a bash mongo --eval?

Here is an example query:
db.readings.find( {"_id.s" : ISODate("2012-11-01T00:05:00Z") } ).count()
Query works in the mongo shell. However, in a bash script or directly in the Ubuntu shell
mongo fivemin --eval "printjson(db.readings.find( {"_id.s" : ISODate("2012-11-01T00:05:00Z") } ).count())"
returns a SyntaxError: missing : after property id (shell eval):1
I can't seem to find a problem with the query. I reverted to { "_id" : {"s" : ...} }, and it still gives the same problem. find().count() works however.
Had the same issue in bash shell.
This will fail because of double quotes:
mongo fivemin --eval "printjson(db.readings.find( {"_id.s" : ISODate("2012-11-01T00:05:00Z") } ).count())"
But using the eval string in single quote it works:
mongo fivemin --eval 'printjson(db.readings.find( {"_id.s" : ISODate("2012-11-01T00:05:00Z") } ).count())'
And if you use a $ sign like $regex you need to escape it:
mongo fivemin --eval 'printjson(db.readings.find( {"_id.s" : {"\$regex":"2012-11-01T*"} } ).count())'
use single quotes into double,
e.g.:
mongo fivemin --eval "printjson(db.readings.find( {'_id.s' : ISODate('2012-11-01T00:05:00Z') } ).count())"
There is also another possible issue using --eval with bash double quotes, if the query contains mongo operators i.e. $ne, $gt etc, since operators start with $.
double quotes:
$ echo " '$ne': null "
$ => '': null
single quotes:
$ echo ' "$ne": null '
$ => "$ne": null
Bash tries to replace these operators with variables.
$ ne = 'test'
$ echo " '$ne': null "
$ => 'test': null
So, I always recommend to use --eval with single quotes.
Just sat and thought about it. It seems to be a problem with bash exiting out on the " (should have noticed immediately!). Instead I used ' (or I guess you can use /" for JSON) so the query looks like:
printjson(db.readings.find({'_id.s' : ISODate('2013-01-01T00:05:00Z') }).count())"
The best way to handle this is to build the mongo command in a var. Then use eval command to execute the mongo command:
mongo_update_query="db.collectionName.update({ name:\""${some_name}"\", \
{ \$addToSet: { nick_names : { \$each : [ ${name_array} ] }}});"
mongo_cmd_str=$(echo "mongo --host ${mongo_host} --port ${mongo_port} ${mongo_database} --eval '${mongo_update_query}'")
# the actual call to mongo query
eval ${mongo_cmd_str}