how to update all the keys in mongoDb document - mongodb

Hi I have a collection that has a complex structure, and the documents in this structures are different in the structure. I want to update all the keys V to have value 0 in this collection.
Example:
{
"_id" : ObjectId("5805dfa519f972b200ea2955"),
"s" : {
"id" : NumberLong(36435)
},
"a" : [
{
"XX-(random value)" : {
"V" : 4
},
"V" : 4,
"u" : {
"YY-(random value)" : {
"V" : 4,
"ZZ-(random value)" : {
"V" : 4,
"WW-(random value)" : {
"V" : 4
}
}
}
}
}
]
}

You can do this with a short javascript. You need to iterate through each property of each document, checking the name of the property. If it matches the name "V" then update it with the value 0. This can be done recursively using a loop. The following javascript code should do the needful:
function checkProperties(doc) {
for (var propertyName in doc) {
if (typeof doc[propertyName] == "object") {
checkProperties(doc[propertyName]);
} else {
if (propertyName == "V") {
doc[propertyName] = 0;
print(doc[propertyName]);
}
}
}
}
db.<your_collection_name>.find({}).forEach(function(doc) {
checkProperties(doc);
db.<your_collection_name>.save(doc);
});
If you save this code in a .js file you can then run it against your mongo database:
> mongo --host <host> --port <port> <script_name>.js

Related

Insert new fields to document at given array index in MongoDB

I have the following document structure in a MongoDB collection :
{
"A" : [ {
"B" : [ { ... } ]
} ]
}
I'd like to update this to :
{
"A" : [ {
"B" : [ { ... } ],
"x" : [],
"y" : { ... }
} ]
}
In other words, I want the "x" and "y" fields to be added to the first element of the "A" array without loosing "B".
Ok as there is only one object in A array you could simply do as below :
Sample Collection Data :
{
"_id" : ObjectId("5e7c3cadc16b5679b4aeec26"),
A:[
{
B: [{ abc: 1 }]
}
]
}
Query :
/** Insert new fields into 'A' array's first object by index 0 */
db.collection.updateOne(
{ "_id" : ObjectId("5e7c3f77c16b5679b4af4caf") },
{ $set: { "A.0.x": [] , "A.0.y" : {abcInY :1 }} }
)
Output :
{
"_id" : ObjectId("5e7c3cadc16b5679b4aeec26"),
"A" : [
{
"B" : [
{
"abc" : 1
}
],
"x" : [],
"y" : {
"abcInY" : 1.0
}
}
]
}
Or Using positional operator $ :
db.collection.updateOne(
{ _id: ObjectId("5e7c3cadc16b5679b4aeec26") , 'A': {$exists : true}},
{ $set: { "A.$.x": [] , "A.$.y" : {abcInY :1 }} }
)
Note : Result will be the same, but functionally when positional operator is used fields x & y are inserted to first object of A array only when A field exists in that documents, if not this positional query would not insert anything (Optionally you can check A is an array condition as well if needed). But when you do updates using index 0 as like in first query if A doesn't exist in document then update would create an A field which is an object & insert fields inside it (Which might cause data inconsistency across docs with two types of A field) - Check below result of 1st query when A doesn't exists.
{
"_id" : ObjectId("5e7c3f77c16b5679b4af4caf"),
"noA" : 1,
"A" : {
"0" : {
"x" : [],
"y" : {
"abcInY" : 1.0
}
}
}
}
However, I think I was able to get anothe#whoami Thanks for the suggestion, I think your first solution should work. However, I think I was able to get another solution to this though I'm not sure if its better or worse (performance wise?) than what you have here. My solution is:
db.coll.update( { "_id" : ObjectId("5e7c4eb3a74cce7fd94a3fe7") }, [ { "$addFields" : { "A" : { "x" : [ 1, 2, 3 ], "y" : { "abc" } } } } ] )
The issue with this is that if "A" has more than one array entry then this will update all elements under "A" which is not something I want. Just out of curiosity is there a way of limiting this solution to only the first entry in "A"?

How to find special characters in a particular attribute of json data stored in mongodb using mongo query

how to search any special characters in a particular nested json object field. Am having a field which stores nested json data.
I need to write a MongoDB query to fetch all the names which is having special characters
Student collection:
Example:
{
_id:123
student: {
"personalinfo":{
"infoid": "YYY21"
"name": "test##!*"
}
}
}
I have tried few regular expressions but I am not sure how to loop in array elements
I expect it to print the infoid & name, which has special characters in the name field.
The following query can do the trick:
db.collection.distinct("student.personalinfo.name",{"student.personalinfo.name": { $not: /^[\w]+[\w ]*$/ } })
Data set:
{
"_id" : ObjectId("5d77a5babd4e75c58d59821d"),
"student" : {
"personalinfo" : {
"infoid" : "YYY21",
"name" : "test##!*"
}
}
}
{
"_id" : ObjectId("5d77a5babd4e75c58d59821e"),
"student" : {
"personalinfo" : {
"infoid" : "YYY21",
"name" : "Bruce##"
}
}
}
{
"_id" : ObjectId("5d77a5babd4e75c58d59821f"),
"student" : {
"personalinfo" : {
"infoid" : "YYY21",
"name" : "Tony"
}
}
}
{
"_id" : ObjectId("5d77a5babd4e75c58d598220"),
"student" : {
"personalinfo" : {
"infoid" : "YYY21",
"name" : "Natasha"
}
}
}
Output:
[ "test##!*", "Bruce##" ]
Try using below regex, it matches all unicode punctuation and symbols.
dbname.find({'student.name':{$regex:"[\p{P}\p{S}]"}})

Update a json document in a mongodb database

I'm trying to update an existing document in a MongoDb. There are many explanations how to do this if you want to update or add key/value pairs on the first level. But in my use-case, I need to create with the first updateOne (with upsert option set) a document with the following structure:
{
"_id" : "1234",
"raw" : {
"meas" : {
"meas1" : {
"data" : "blabla"
}
}
}
}
In the second command, I need to add - in the same document - a "meas2" field at the level of "meas1". My desired output is:
{
"_id" : "1234",
"raw" : {
"meas" : {
"meas1" : {
"data" : "blabla"
},
"meas2" : {
"data" : "foo"
}
}
}
}
I played with statements like
updateOne({"_id":"1234"},{$set:{"raw":{"meas":{"meas2":{"data":"foo"}}}}}, {"upsert":true})
and also with $push, both variants with insert - here only the document and also insertOne, but nothing produces the desired output. Is there a MongoDb expert who could give a hint ? ... I'm sure this functionality exists... Thanks in advance!
When you update {$set: {"raw":{"meas":{"meas2":{"data":"foo"}}}} you're not adding "mesa2" to "meas" but rather you're overriting "raw" completely.
In order to change / add one field in a document refer to it with dot notations.
The command you want is updateOne({"_id": "1234"}, {$set: {"raw.meas.mesa2": { "data" : "foo" }}}, {"upsert":"true"})
You need to understand the below concept first
Set Fields in Embedded Documents, with details document check at official documentation of mongo
For your problem, just look at the below execution on the mongo shell:
> db.st4.insert({
... "_id" : "1234",
... "raw" : {
... "meas" : {
... "meas1" : {
... "data" : "blabla"
... }
... }
... }
... })
WriteResult({ "nInserted" : 1 })
> db.st4.find()
{ "_id" : "1234", "raw" : { "meas" : { "meas1" : { "data" : "blabla" } } } }
>
> // Below query will replace the raw document with {"meas":{"meas2":{"data":"foo"}}}, will not add
> //db.st4.updateOne({"_id":"1234"},{$set:{"raw":{"meas":{"meas2":{"data":"foo"}}}}}, {"upsert":true})
>// By using the dot operator, you actually write the values inside the documents i.e you are replacing or adding at raw.meas.mesa2 i.e inside the document of mesa2.
> db.st4.updateOne({"_id":"1234"},{$set: {"raw.meas.mesa2": { "data" : "foo" }}}, {"upsert":"true"})
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
> db.st4.find().pretty()
{
"_id" : "1234",
"raw" : {
"meas" : {
"meas1" : {
"data" : "blabla"
},
"mesa2" : {
"data" : "foo"
}
}
}
}
>

MongoDB - how to dynamically change key

I am new to MongoDB. I was wondering how MongoDB implements "variable Interpolation" ?
I have following code
for (i=0;i<3;i++){
db.test2.insert({i:i+1});
}
Which insert
{ "_id" : ObjectId("564a1bd0987fe676b9cad025"), "i" : 1 }
{ "_id" : ObjectId("564a1bd0987fe676b9cad026"), "i" : 2 }
{ "_id" : ObjectId("564a1bd0987fe676b9cad027"), "i" : 3 }
But I want
{ "_id" : ObjectId("564a1bd0987fe676b9cad025"), "0" : 1 }
{ "_id" : ObjectId("564a1bd0987fe676b9cad026"), "1" : 2 }
{ "_id" : ObjectId("564a1bd0987fe676b9cad027"), "2" : 3 }
Mongo does not changes value in key part. How to fit it?
You will need to build your query dynamically.
var documents = [];
for(var i=0; i<3; i++) {
var doc = {};
doc[i] = i;
documents.push(doc);
}
db.collection.insert(documents)
Then db.collection.find() yields:
{ "_id" : ObjectId("564a2296c68c7068c12fb206"), "0" : 0 }
{ "_id" : ObjectId("564a2296c68c7068c12fb207"), "1" : 1 }
{ "_id" : ObjectId("564a2296c68c7068c12fb208"), "2" : 2 }
Note that it's not a good practice to have string of integer as field's name

array projection with findandmodify

I'm using findAndModify command to maintain a list of undo/redo commands. I'm using "fields" tag to specify what the "before" values were. This is then used to build undo command. ie:
cmd:
{
findAndModify : "aaa",
query : { _id: ObjectId('5215f7d1fe789bb17427bde9') },
update : { "$set" : { "v1" : 200 } },
fields : { v1 : 1, _id : 0 }
}
built result:
"Do" : { "$set" : { "v1" : 200 } },
"Undo" : { "$set" : { v1" : 100 } }
However, I cannot make this work for arrays. I do a set command like this:
{ $set : "myArrayVar.3" : 100 }
I've tried using projections like:
{ "myArrayVar.3" : 1 }
{ "myArrayVar.$" : 1 }
but both return just empty array braces:
{ myArrayVar : [] }
What am I missing?
You can't use numeric array indexes in projections, but you can use $slice instead:
fields: { myArrayVar: { $slice: [2, 1] } }
That would include just the third element (skip 2, take 1).