How to make duplicate records - mongodb

I want to duplicate my all records in my collection of mongodb.
eg:
my collection have 1500 records so i need to change it into 3000.
means double
I need any query which can do this in one command

You can find everything using find then use foreach loop to insert it again.
db.col.find({},{_id:0}).forEach((doc)=>{db.col.save(doc)});
Make sure to remove _id because every duplicate is going to have its own unique _id

Related

mongodb multiple documents insert or update by unique key

I would like to get a list of items from an external resource periodically and save them into a collection.
There are several possible solutions but they are not optimal, for example:
Delete the entire collection and save the new list of items
Get all items from the collection using "find({})" and use it to filter out existing items and save those that do not exist.
But a better solution will be to set a unique key and just do kind of "update or insert".
Right now on saving items the unique key already exists I will get an error
is there a way to do it at all?
**upsert won't do the work since it's updating all items with the same value so it's actually good for a single document only
I have a feeling you can achieve what you want simply by using the "normal" insertMany with the ordered option set to false. The documentation states that
Note that one document was inserted: The first document of _id: 13
will insert successfully, but the second insert will fail. This will
also stop additional documents left in the queue from being inserted.
With ordered to false, the insert operation would continue with any
remaining documents.
So you will get "duplicate key" exceptions which, however, you can simply ignore in your case.

merge rows in pentaho PDI kettle

I was wondering if I could merge 2 or more rows in pentaho?
example:
I have 2 rows of
case:'001',
owner:'Barack'
date:'2017-04-10'
case:'001',
owner:'Trump'
date:'2017-02-10'
Then I want to have a mongoDB output :
case:'001'
ownerHistory:[
{
owner:'Barack'
date:'2017-04-10'
},
{
owner:'Trump'
date:'2017-02-10'
}
]
The MongoDB Output step supports modifier updates that affect individual fields in a document and the $push operation to add items to a list/array. Using those, you don't need to merge any rows, you can just send them all to the MongoDB Output step.
With the Modifier update active each next row will be either inserted if the case doesn't exist yet or have the owner added to the array if it does.
Unfortunately, if you run this transformation a second time with the same incoming data, it will add more copies of the owners. I haven't found a way to fix that, so I hope you don't have that in your use case.
Perhaps if you split the data and insert/replace the using the first record for each case, then do the $push updates for the second and later records you can manage it.

Mongodb, find last record, does the total count affect the performance?

I have a mongodb collection only insert and find last record will be issued against it.
And the count of records of this collection is very big, will this affect the time of find last record? Or maybe the affect is negligible?
the query used to find last record:
db.col.find().sort({created: -1}).limit(1)
try this one work
db.collection.find().limit(1).sort({$natural:-1})

create unique id in mongodb from last inserted id using pymongo

Is there a way I can find the last inserted document and the field, i.e. _id or id such that I can increment and use when inserting a new document?
The issue is that I create my own id count, but I do not store this, now I've deleted records, I cannot seem to add new records because I am attempting to use the same id.
There is no way to check insertion order in MongoDB, because the database does not keep any metadata in the collections regading the documents.
If your _id field is generated server-side then you need to have a very good algorithm for this value in order to provide collision avoidance and uniqueness while at the same time following any sequential constraints that you might have.

MongoDB update many documents with different timestamps in one update

I have 10000 documents in one MongoDB collection. I'd like to update all the documents with datetime values that are 1 second apart for each document (so all the date time values are unique and are spaced 1 second apart). Is there any way to do this with a single update instead of updating each document in turn which results in 10000 distinct update operations?
Thanks.
No, there is no way to do this with a single update statement. There are no expressions which run at the server to allow this type of update. There is a feature request for this but it is not done so it cannot be used.