How to remove document from all collections in Mongo database - mongodb

I have Mongo database with 16 collections. All collection has the common field domain_id.
How can I remove documents with specified domain_id from all collections.
I know only how to remove document from single collection.
db.getCollection('collectionName1').remove({domain_id : '123'})

Use the method db.getCollectionNames() to get a list of all the collections in your database, iterate the list using the JavaScript's forEach()
method to remove the document from each collection:
db.getCollectionNames().forEach(function (col) {
db.getCollection(col).remove({domain_id : '123'})
});

Unfortunately, Mondo doesn't allow for linking collections. So, you do have to do it for each separate collection.

Related

Mongodb: searching embedded documents by the '_id' field

If I have a data with a structure like this as a single document in a collection:
{
_id: ObjectId("firstid"),
"name": "sublimetest",
"child": {
_id: ObjectId("childid"),
"name": "materialtheme"
}
}
is there a way to search for the embedded document by the id "childid" ?
because mongo doesn't index the _id fields of embedded documents (correct me if I am wrong here),
as this query doesn't work :
db.collection.find({_id:"childid"});
Also please suggest me if there is any other document database that would be suitable for this kind of retreiving data that is structured as a tree, where the requirement is to :
query children without having to issue joins
find any node in the tree as fast as you would find the root node, as if all these nodes were stored as separate documents in a collection.
Why this is not a duplicate of question(s) suggested :
the potential-duplicate-question, queries document by using dot notation. But what if the document is nested 7 levels deep ? In such case it would not be suitable to write a query using dot notation. what I want is that, all documents, whether top level, or nested, if they have the _id field, should be in the bucket of _id indexes, so that when you search db.collection.find({_id: "asdf"}), it should take into account documents that are nested too that have the _id field matching "asdf". In short, it should be as if the inner document weren't nested, but present parallel to the outer one.
You can use the dot notation:
db.posts.find({"child._id": "childid"})

how to prevent a document getting indexed during full import?

I have a mongodb collection which i would like to index on solr using data import handler . But i don't want the documents with foodType(a field in document) as variant to get indexed during full import how to do it?
If you have the query to get the documents from the MongoDB.
Then modify that query by adding a filtering condition.
This filter will not retreive the data from mongoDB where the documents has foodType.
e.g
db.collection.find({ "fieldToCheck" : { $exists : true, $ne : null } })
You can add some conditions as above in your query and restrict the data from being indexed in solr.
In the data-config the query is missing and that is the reason its getting all the documents from mongoDB. If you add the query with correct criteria it will retrive selected documents.
For more on the mongo Query please refer the link below
MOngoDB reference link

mongodb query on DBRef type

How do I turn this query in a valid mongodb Query in mongodb shell.
{ 'cars.owner.$ref' : 'users' }
cars.owner is a DBRef here, but $ref is invalid
I get this error:
"$err" : "Positional operator does not match the query specifier."
My objective here is to figure out if there are any cars "owned" by different collections then users.
You can query the DBRef in the Mongo shell, but you have to use the DBRef() function. The reference must include at least the $ref and $id. From the docs:
DBRef documents resemble the following document:
{ "$ref" : <value>, "$id" : <value>, "$db" : <value> }
When cars.owner is a reference to a document in the users collection, a query to find all cars where owner is a certain _id might look like (assuming both collections are in the same database):
db.cars.find({ "owner" : DBRef("users", ObjectId("<user _id value>")) })
The $ref and $id values cannot be queried directly. The DBRef is most useful in the case where there are multiple references to different collections in the same document. Using DBRef might be overkill when there is only one reference in the document, as others have mentioned.
If you need to reference different collections in your owners field, you are probably better served by using separate owner_collection and owner_id fields. The query to find all owners that are not users would then be a standard query:
db.cars.find({ owner_collection: { $ne: "users" } })
MongoDB documentation (database-reference) says following:
MongoDB applications use one of two methods for relating documents:
Manual references where you save the _id field of one document in another document as a reference. Then your application can run a second query to return the related data. These references are simple and sufficient for most use cases.
DBRefs are references from one document to another using the value of the first document’s _id field, collection name, and, optionally, its database name. By including these names, DBRefs allow documents located in multiple collections to be more easily linked with documents from a single collection. To resolve DBRefs, your application must perform additional queries to return the referenced documents. Many drivers have helper methods that form the query for the DBRef automatically. The drivers 1 do not automatically resolve DBRefs into documents.
I don't know about any plans, but I have read that DBRefs should be deprecated(?) nowadays. However, you can use fetch() -method to load referenced documents. For example, I have formView collection which stores documents that contains (DBRef) references to documents in formContent collection. So, running:
var view = db.formView.findOne()
view.formContent
view.formContent.fetch()
view.formContent.fetch().code
...Will result in following output:
DBRef("formContent", ObjectId("56cb155ea826872b67373e76"))
{
"_id" : ObjectId("56cb155ea826872b67373e76"),
"code" : "test_content",
"version" : 0
}
test_content
<parameterName>: DBRef("CollectionName", "_id Value")
put in collection.Then Create a Bean having feild name as parameterName, and put #Reference annotation of morphia orm then do operations you want

MongoDB return all the fields in a collection

Is there a query for returning all the fields (keys) that exist in a collection?
Due to the fact that MongoDB has a flexible document structure, the fields change from doc to doc in a collection, I would like to query for all the fields in a collection.
f.e. {"color":"red", "shape":"round", "radius":3}, {"color":"green", "shape":"square", "length":2, "width":3}
This docs are in the same collection. I would like the query to return - {"color", "shape", "radius", "length", "width"}
Thank you.
Due to the fact that each document in a collection can have a different numbers of keys there is no good way of doing this, MongoDB holds no (unlike SQL) meta data about what keys exist within the collection.
There are however a few tools that be of help here:
https://github.com/variety/variety
https://github.com/skratchdot/mongodb-schema/
And here is a related question with some answers:
MongoDB Get names of all keys in collection

In MongoDB, do document _id's need to be unique across a collection or the entire DB?

I'm building a database with several collections. I have unique strings that I plan on using for all the documents in the main collection. Documents in other collections will reference documents in the main collection, which means I'll have to save said id's in the other collections. However, if _id's only need to be unique across a collection and not across an entire database, then I would just make the _id's in the other collections also use the aforementioned unique strings.
Also, I assume that in order to set my own _id's, all I have to do is have an "_id":"unique_string" property as part of the document that I insert, correct? I wouldn't need to convert the "unique_string" into another format, right?
Also, hypothetically speaking, would I be able to have a variable save the string "_id" and use that instead? Just to be clear, something as follows: var id = "_id" and then later on in the code (during an insert or a query for example) have id:"unique_string".
Best, and thanks,Sami
_ids have to be unique in a collection. You can quickly verify this by inserting two documents with the same _id in two different collections.
Your other assumptions are correct, just try them and see whether they work (they will). The proof of the pudding is in the eating.
Note: use _id directly, var id = "_id" just compilcates the code.