find and replace string in all mongodb collections - mongodb

I have a mongodb database named "eagle" and am trying to replace all email records of "blue#domain.com" with "pink#domain.com" (within collection "all collections".
db.eagle.find({}).forEach(function(e,i) {
e.email=e.email.replace("//blue#domain.com","//pink#domain.com");
db.eagle.save(e);
});
I am very new to mongodb...so I'm not even sure that I'm in "eagle" or "falcon"....I just run mongo. This query doesn't do anything.
Here is what my Communications object looks like:
{
"_id" : ObjectId("redacted"),
"timestamp" : ISODate("2016-08-03T15:08:07.000Z"),
"thread_index" : "",
"updated_at" : ISODate("2016-09-01T17:49:31.401Z"),
"from" : {
"username" : "None",
"name" : "Pinky Jones",
"email" : "blue#domain.com"
},
"to" : {
"username" : "redude",
"name" : "Red Baron",
"email" : "red#domain.com"
},
"created_at" : ISODate("2016-09-01T17:49:31.401Z"),
}

Use findAndModify instead find
Documentación findAndModify

Related

MongoDB : find all entries with one key

I am trying to use mongoDB on a new project.
I have this documents in my database :
{ "_id" : ObjectId("ID"), "local" : { "rank" : "null", "password" : "PASSWORD", "email" : "EMAIL" }, "__v" : 0 }
{ "_id" : ObjectId("ID"), "local" : { "rank" : "admin", "password" : "PASSWORD", "email" : "EMAIL" }, "__v" : 0 }
{ "_id" : ObjectId("ID"), "local" : { "rank" : "null", "password" : "PASSWORD", "email" : "EMAIL" }, "__v" : 0 }
I want to find all documents with a null rank.
So I test :
db.users.find({local:{rank:"null"}})
But it doesn't work.
If I test with the command :
db.users.find({"local":{ "rank" : "null", "password" : "PASSWORD", "email" : "EMAIL" }})
It works. Why it doesn't work with the first command ?
Both comparisons you are doing against document. The second one works it matches document local.
To compare specific field inside the document you have to use dot notation.
db.users.find({"local.rank":"null"}).
More about how to query embedded document. https://docs.mongodb.com/manual/core/document/#embedded-documents

query is not notworking

I am using mongodb as my database and using scala for developing and i am new to this.
Now i have a problem in updating the database using a query
collectionProfile.update(BSONDocument("handle"->handle,"active"->true), BSONDocument("$set"->BSONDocument("messages.received"->BSONDocument("isRead"->true))))
and here is my collection
{
"_id" : ObjectId("5877969d1300005801327fa8"),
"handle" : "sd3423dwwrewer342",
"active" : true,
"messages" : {
"received" : [
{
"id" : "u-bf130421-418b-41a5-8965-0e7691b9412a",
"by" : "S_0ECB8108_7771_4701_9813_D7D360B994AC",
"to" : "J_04ABD48A_28D2_44CD_9D05_76C709E50724",
"subject" : "hi",
"content" : "hello",
"time" : "2017-01-12T20:16:25.268",
"isRead" : false
}
]
}
}
here i am trying to update the isRead variable true but it is not working can anyone help me with this query.

How to generate mongodb object id for a sub-document on insert

Inserting a document that also has sub-documents. I am doing this through cli. How do I make mongodb generate a object id for the Alerts sub-document record? I want something like the following, but this does not work from cli.
db.user.insert(
{
"Email" : "andy+5#domain.com",
"Domain" : "other",
"Type" : "local",
"FName" : "Andy",
"Alerts" : [
{
"_id" : new ObjectId(),
"Keyword" : "sales",
"Frequency" : {
"Type" : "daily"
},
"IsActive" : true
},
{
"_id" : new ObjectId(),
"Keyword" : "customer service",
"Frequency" : {
"Type" : "daily"
},
"IsActive" : true
}
]
}
)
You can run that exact script on the console on robomongo, it would execute just fine. Don't do it on add or edit dialog.

MongoDB query insert field into document from a list of Id's

I'm kind of stuck with the following problem. I have a MongoDB filled with documents, of these documents (I have a list with Id's) I need to insert a field.
I have this document:
{
"id" : 3639,
"type" : "P",
"createdate" : "2011-10-19T11:45:14+0200",
"name_creator" : "",
"latitude" : "50.887",
"longitude" : "9.14999",
"themes" : [{
"name" : "Fun",
"id" : "4"
}, {
"name" : "Kids",
"id" : "5"
}]
}
I need a query the can insert the themes field into the document, the current themes field does not have to be updates, just 1 new one. I have over 300 Id's where this has to be done.
The document should then look like this:
(all the other fields in themes should be removed, just one new one 'Outside')
{
"id" : 3639,
"type" : "P",
"createdate" : "2011-10-19T11:45:14+0200",
"name_creator" : "",
"latitude" : "50.887",
"longitude" : "9.14999",
"themes" : [{
"name" : "Outside",
"id" : "6"
}]
}
I would normally write a bit of Java code that would loop over the documents and change them, but I believe (hope) this could be done in a query.
Anyone got an idea on how I could do this?
Thanks for any help!
All you need to do is
db.collection.update(
{id : {$in : [your list of ids]}},
{$set : {
theme : [{
"name" : "Outside",
"id" : "6"
}]
}},
{multi : true}
)

Two MongoDB queries same result, what is the difference?

I'm playing around with mongoDB and I can't figure out what is the difference between my two queries.
I use following collection:
{
"_id" : ObjectId("520b79869971eb1a0fdd0ad4"),
"created" : 1376483718636,
"updated" : 1376483718636,
"firstName" : "Jakob",
"lastName" : "D",
"email" : "jakob.d#test.com",
"emailValidated" : false,
"phoneNumber" : "",
"lastLogin" : 1376483718624,
"linkedProviders" : [
{
"userId" : "1XXXXXXXX6",
"providerId" : "facebook",
"password" : "",
"salt" : "",
"authMethod" : "oauth2",
"avatarUrl" : ""
}
],
"userRoles" : [
"ADMIN"
]
},
{
"_id" : ObjectId("520b7dd09971ebcd35dd0ad6"),
"created" : 1376484816666,
"updated" : 1376484816666,
"firstName" : "Jakob",
"lastName" : "D",
"email" : "jakob.d#test.com",
"emailValidated" : false,
"phoneNumber" : "",
"lastLogin" : 1376484816666,
"linkedProviders" : [
{
"userId" : "jakob.d#test.com",
"providerId" : "userpass",
"password" : "7e4aff9e0d90db2318ffcc689c11b66d",
"salt" : "N1GgNvy3NnS0i5GFDyglQZ9s4CeFNndn",
"authMethod" : "userPassword",
"avatarUrl" : ""
}
],
"userRoles" : [
"ADMIN"
]
}
The two queries that gives me the correct and same result(the one with objectId 520b79869971eb1a0fdd0ad4) are:
db.users.find({"linkedProviders.userId":"1XXXXXXXX6","linkedProviders.providerId":"facebook"})
db.users.find({"linkedProviders": {"$elemMatch": {"userId":"1XXXXXXXX6" },"$elemMatch": {"providerId":"facebook" }}})
So what is the difference between these two?
The difference is that $elemMatch finds items for one single array element.
This solution:
db.users.find({
"linkedProviders.userId": "1XXXXXXXX6",
"linkedProviders.providerId": "facebook"
})
Finds any user that has that userId and that providerId, but possibly in different items in linkedProviders, e.g., if linkedProviders[0].userId matches the first and linkedProviders[1].providerId matches the second part of the query, the full document (i.e., the user) will match that query.
On the other hand,
db.users.find({
"linkedProviders": {
"$elemMatch": {
"userId": "1XXXXXXXX6",
"providerId": "facebook"
}
}
})
will match only if the index values (0 and 1 in the previous example) are the same in the document, i.e., only if one array element matches both.
When there's only one key-value mapping in the $elemMatch, it should do the same as the query with the key-value mapping applied directly.
More information:
http://docs.mongodb.org/manual/reference/operator/elemMatch/#op._S_elemMatch