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

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 .

Related

Mongoose schema definition [duplicate]

This question already has an answer here:
Why does mongoose use schema when mongodb's benefit is supposed to be that it's schema-less?
(1 answer)
Closed 5 years ago.
I am a beginner with MongoDB and trying to learn MEAN Stack. So I am using Mongoose as the ORM
I read that MongoDB is a NoSQL database, but while using Mongoose as ORM, I am asked to create a schema first. Why is it so? There shouldn't be a schema ideally as MongoDB is a NoSQL database.
Thanks in advance.
Mongoose is an orm on top of mongodb , if you are using core mongodb you need not create any schema , you can just dump any data you want , but in mongoose you have a schema so that you can i have some basic key value pair for advanced searching and filtering and you can anytime update the schema. Or If you want to go schemaless and dump whatever the response is you can use a schema type like this var someSchema = {data:Object} and drop all your data in this data key and then you can easily extract whatever JSON data is inside your id field.
var mongoose = require('mongoose');
module.exports = mongoose.model('twitter', {
created_at:{
type:Date
},
dump:{
type:Object
}
});
In the above example dump is used to save whatever JSON I get as a response from twitter api and created_at contains only the creating date of tweet , so I have the entire data , but if i want to search tweets of a particular date I can search it using a find query on created_at and this query will be lot faster and here I have a fixed structure and a knowledge about what to expect of a find query each time a run one, So this is one of the benefit of using the mongoose orm i.e I don't lose data but I can maximise my searching ability by creating appropriate keys.
So basically mongoose is an ORM db , it offers you relational db features like creating foreign keys , not strictly foreign keys but you can create something like an id reference to another schema and later populate the field by the id associated parameters when you fetch data using your find query , also a relational schema is easy to manage , what mongoose does is it gives a JSON/BSON based db the power of relational db and you get best of both the world i.e you can easily maintain new keys or you don't need to worry about extracting each and every data from your operation and placing it properly/inserting it , you just need to see that your keys and values match , as well as you have flexibility in update operations while having a schema or table structure.

mongo-hadoop. not to handle mongodb document deletion

I want to synchronize mongodb and hadoop, but when I delete document from mongodb, this document must not be deleted in hadoop.
I tried using mongo-hadoop and hive. this is hive query:
CREATE EXTERNAL TABLE SubComponentSubmission
(
id STRING,
status INT,
providerId STRING,
dateCreated TIMESTAMP,
subComponentId STRING,
packageName STRING
)
STORED BY 'com.mongodb.hadoop.hive.MongoStorageHandler'
WITH SERDEPROPERTIES('mongo.columns.mapping'=
'{"id":"_id", "status":"Status",
"providerId":"ProviderId",
"dateCreated":"DateCreated",
"subComponentId":"SubComponentPackage.SubComponentId",
"packageName":"SubComponentPackage.PackageName"}'
)
TBLPROPERTIES('mongo.uri'='mongodb://<host>:27017/<db name>.<collection name>');
this query creates table that is synchronized to corresponding mongodb collection. by this query mongo-hadoop handles document deletion too.
does mongo-hadoop have any option, not to handle document deletion?
or, is there any other tool that solves this problem?
thanks in advance.
If you query directly against mongo like you're doing, yes, you're going to see all the document mutations that happen in mongo. That's the whole point of querying against mongo like this. If you want snapshotted views of your mongo data, you'll need to do something like a mongodump and putting the bson files on disk somewhere (like HDFS). Otherwise you'll always be querying against the live, mutating data.

Does not show any collection in mongodb

I am a beginner in mongodb. 2 days before I created a db named inventory and inserted collection too. But today I want get all collections in Inventory
I typed
db.inventory.find()
but it didn't show anything... what's the reason?
If ur db is inventory just use the following commands
use inventory
show collections
This would list u all the collections u've created inside this db .
db.collection_name.find()
will list u all data(documents) u've created in it
In short:
use inventory
show collections
By default the mongo shell connects to the database test. so you have to type use inventory to switch to your desired database (show databases returns a list of all created databases if you are facing errors with typos). After switching to the correct database type show collections to get a list of all created collections in your current database.

MongoDB : alter an existing index on a collection

We are using MongoDB for our Application .
I see that an indexes are already present for the collections .
We had a new requirement , for which i see that the response is very slow from MongoDB for some of the opeartioons.
I want to add an extra field to the existing index , without dropping the existing index .
Please tell me if this is possible or not .
Will this have any impact on the Application ??
I am pretty sure you can't alter indexes once they have been created.
You could easily create a new index that would cover the new field.
Adding indexes is very easy from the shell you can type
db.yourcollection.ensureIndex( { field1: 1, field2: -1 } )
Mongo will look at your indexes and work out which one is best to use for your query. You can see this by adding explain onto the end of your query in the shell, this will tell you if it used an index and what index was used.
This will also be a good tool for working out what is slow about your query.
See the Mongo Documentation for further details http://docs.mongodb.org/manual/core/indexes/

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())