Bulk insert in Mongodb and Postgresql - mongodb

I am doing a comparison on Postgresql vs Mongodb on insert.I am using the copy command (from file to table) in Postgresql and i am wondering what is the equivalent command in mongodb.I think that the answer is the insert_many(dict)But i want be sure.Any advice would be appreciated.Thanks in advance!

If you want to insert multiple documents in mongodb, you can use
db.collectionName.insertMany([{title: 'doc 1'},{title: 'doc2'},......]}

Related

How to insert the data into mongoDB collection manually

I want to insert the data into Robo3T manually. I tried using insert and insertMany command, but nothing works for me. Please provide me the solution if it's possible.
db.getCollection('YourCollectionName').insert({"name" : "ABC"})

Find hashed data with MongoDB

Is it possible with MongoDB to find data by its hash?
I'm trying to find the MongoDB equivalent of this MySQL query:
SELECT column FROM table WHERE SHA1(column) = "value"
It doesn't seems there is any such functionality (a bit old, but couldn't find in docs.mongodb.com either), but you can include a library that contains sha1 functionality (e.g. js-sha1) in mongo shell with load function, then use it within your mongo operation.

Alternative for mongodb $push function in postgres

Converting the MongoDb queries into postgresql.
In mongoDb we the basic usage of $push is as follows :
https://docs.mongodb.org/v3.0/reference/operator/aggregation/push/
Is there any alternative for this in postgresql or we need to implement it using the code.
You can do this using array_agg function in Postgres.
See Sample code(shamelessly copied ) : http://www.sqlfiddle.com/#!15/21b2b/1

Delete data from a collection in mongo db by using multiple conditions

I am new to mongo db
using i have to delete data from a table like
select from table where id=12 and browser =GC
But I dont know how to perform this in mongo db?
Use remove
db.collectionName.remove({'_id':12,'browser':"GC"})
Can provide more accurate answer if you show your collection and documents .

Is there a better way to export a mongodb query to a new collection?

What I want:
I have a master collection of products, I then want to filter them and put them in a separate collection.
db.masterproducts.find({category:"scuba gear"}).copyTo(db.newcollection)
Of course, I realise the 'copyTo' does not exist.
I thought I could do it with MapReduce as results are created in a new collection using the new 'out' parameter in v1.8; however this new collection is not a subset of my original collection. Or can it be if I use MapReduce correctly?
To get around it I am currently doing this:
Step 1:
/usr/local/mongodb/bin/mongodump --db database --collection masterproducts -q '{category:"scuba gear"}'
Step 2:
/usr/local/mongodb/bin/mongorestore -d database -c newcollection --drop packages.bson
My 2 step method just seems rather inefficient!
Any help greatly appreciated.
Thanks
Bob
You can iterate through your query result and save each item like this:
db.oldCollection.find(query).forEach(function(x){db.newCollection.save(x);})
You can create small server side javascript (like this one, just add filtering you want) and execute it using eval
You can use dump/restore in the way you described above
Copy collection command shoud be in mongodb soon (will be done in votes order)! See jira feature.
You should be able to create a subset with mapreduce (using 'out'). The problem is mapreduce has a special output format so your documents are going to be transformed (there is a JIRA ticket to add support for another format, but I can not find it at the moment). It is also going to be very inefficent :/
Copying a cursor to a collection makes a lot of sense, I suggest creating a ticket for this.
there is also toArray() method which can be used:
//create new collection
db.creatCollection("resultCollection")
// now query for type="foo" and insert the results into new collection
db.resultCollection.insert( (db.orginialCollection.find({type:'foo'}).toArray())