How to index array data type from mongodb to elasticsearch using logstash - mongodb

We were trying to index data from mongodb using logstash but we were unable to index array data type fields alone, also there were no errors in the log file.

It was an issue with MongoDB plugin which we used in logstash.
we added ruby code to the logstash-conf file to get the arrays from log_entry field.
Note: log_entry will have the complete field list and data.

Related

How to view entire array in MongoDB shell?

I am doing a Mini project in MongoDB recently. I am trying to find out the distinct values of field using the db.collections.distinct(<field>)command.
The above command retrieves all the distinct values of the given field inside an array. But as I have 12k documents all the values are not getting displayed in my console.
This is the output I get.. how to view the entire result?
I have search the internet and documentation but I was not able to get a proper answer.
The mongosh is a Node.js environment. There are several solutions, e.g. JSON.stringify the array or use util.inspect.
JSON.stringify(db.collections.distinct(<field>))
util.inspect(db.collections.distinct(<field>), { maxArrayLength: null })

Unable to to copy one Field to another filed Mongo db Native query

Hi guys unable to copy one field value to another field in mongo db update many options, I am using mongo db 3.6, i attached my query below pls help me to overcome this issue.
db.am_task.updateMany(
{"reselleraccountid":{$exists:true}},
{$set:{"reselleraccountid":"$parantid"}}
)
Actual Results:
Expected Result
its should replace its value, not print its key name.

MongoDB - querying GridFS by metadata does not return any results

I am trying to query MongoDB database for a file stored in GridFS using metadata in the following way:
db['fs'].files.find({'metadata': {'a_field': 'a_value'}})
And it does not return any results whereas I can see the file with such a field value exists when I run e.g.:
db['fs'].files.find()
What is wrong about my query?
It turns out the problem is solved by changing the nesting of JSON query document from:
{'metadata': {'a_field': 'a_value'}}
to:
{'metadata.a_field': 'a_value'}
It is still a mystery to me why the two queries are not equivalent, though.

How to serarch from JSON data with MongoDB

I am new in MongoDB. I am using MongoDB 3.6.3. I have one fields in the document with huge josn data and I don't know all key names present inside JSON data.
Now I want to search documents by the value which will present in any key inside JSON data.
I have tried using this.
db.getCollection('booking').find({'result.important_information': /.*small dogs*/})
But this required key for search But I dont have key name I have to search using the only value.

fill up mongo data automatically by using script

I am a newbie to mongo, I have a collection in my mongodb, To test a feature in my project I need to update database with some random data.I need a script to do that. by identifying the datatype of the field script should fill up the data automatically.
suppose I have the fields in the collection:
id, name, first_name, last_name, current_date, user_income etc.
Since the my questions are as follows:
1. Can we get all field names of a collection with their data types?
2. Can we generate a random value of that data type in mongo shell?
3. how to set the values dynamically to store random data.
I am frequently putting manually to do this.
1. Can we get all field names of a collection with their data types?
mongodb collections are schema-less, which means each document (row in relation database) can have different fields. When you find a document from a collection, you could get its fields names and data types.
2. Can we generate a random value of that data type in mongo shell?
3. how to set the values dynamically to store random data.
mongo shell use JavaScript, you may write a js script and run it with mongo the_js_file.js. So you could generate a random value in the js script.
It's useful to have a look at the mongo JavaScript API documentation and the mongo shell JavaScript Method Reference.
Other script language such as Python can also do that. mongodb has their APIs too.