findAndModify an Object Using Variable Key - mongodb

I am trying to update a record which has a variable key and a pre-existing object associated with that key, as such:
[variable_name] : {
date : 'XXXX'
}
How can I use findAndModify() to insert more key-values into that structure, without removing the existing data? I could not figure it out using $set and an update object that resembled updateObj[variable_name] = { newProp : newData }.

Use the $set operator together with the dot notation to specify a new field in an embedded document. For example, since the newProp field does not exist and you specify a dotted path for that non-existent field, $set will add the new field with the specified value thus creating the embedded documents as needed to fulfill the dotted path to the field. The {upsert: true} flag creates a new document when no document matches the query criteria. The default value is false, which does not insert a new document when no match is found:
db.collection.update(
{ "variable_name.date": ISODate("2015-09-23T18:06:00.696Z") },
{ $set: { "variable_name.newProp": "zzz" } },
{ "upsert": true }
)

Too late of an answer for the question but hope it helps others.
What you can do is the following, using Template Literals and Computed Property
const key = `${variable_name}.newProp`;
db.update({_id: myId}, { $set: { [key]: newData } });

Related

How to change attribute name of the embed type?

How I can change the name of the embed type by query $rename?
{ list_name: [{ name_1: String }] } => { list_name: [{ name_2: String }] }
I tried db.getCollection('test').updateMany({}, { $rename: { 'list_name.name_1': 'name_2' }})
But it's throwing an error: WriteError: cannot use the part (list_name of list_name.name_1) to traverse the element ({list_name: [ { name_1: "test" } ]})
$rename does not work if these fields are in array elements
Refer
To achieve, you need $unset and $set document by document. You can use bulkwrite also.
list_name is an array. Hence $rename didn't work.
When I search, I get this. You can refer the second answer if you have less number of docs.
Else, you need to use either bulk write or mongo dump options.

Replacing type in MongoDB

I have a MongoDB database where I'm trying to replace all arrays of a particular element with a concatenated string. Currently, most of the documents have an array of strings. I need a script that will find each of the documents that have an array (some have been updated by hand already) and replace that array with a concatenation of what's currently there.
So far, I have:
var cursor = db.recipe.find({ directions: { $type : 4 } });
while ( cursor.hasNext() ) {
var doc = cursor.next();
db.recipe.update(
{ _id : doc._id },
{ $set : { directions : { $concat : { doc.directions } } } }
);
};
Unfortunately, it keeps complaining about an unexpected '.'. I'm assuming that I'm using $concat incorrectly.
$concat is an aggregation operator. To simply find and update, use plain javascript.
Currently, most of the documents have an array of strings. I need a script that will find each of the documents that have an array
When you apply $type:4 to an array, it in turn checks if any of the elements inside the directions field is an array, only if it finds one it returns true.
eg: for input ["x","y"] it would return false, and for [["x"],"y"] it would return true.
Since your array contains only strings, you need to use the $where operator to find the type.
db.recipe.find({$where:"Array.isArray(this.directions)"}).forEach(function(doc){
var value = doc.directions.join();
db.recipe.update({"_id":doc._id},{$set:{"directions":value}});
})

Override existing Docs in production MongoDB

I have recently changed one of my fields from object to array of objects.
In my production I have only 14 documents with this field, so I decided to change those fields.
Is there any best practices to do that?
As it is in my production I need to do it in a best way possible?
I got the document Id's of those collections.like ['xxx','yyy','zzz',...........]
my doc structure is like
_id:"xxx",option1:{"op1":"value1","op2":"value2"},option2:"some value"
and I want to change it like(converting object to array of objects)
_id:"xxx",option1:[{"op1":"value1","op2":"value2"},
{"op1":"value1","op2":"value2"}
],option2:"some value"
Can I use upsert? If so How to do it?
Since you need to create the new value of the field based on the old value, you should retrieve each document with a query like
db.collection.find({ "_id" : { "in" : [<array of _id's>] } })
then iterate over the results and $set the value of the field to its new value:
db.collection.find({ "_id" : { "in" : [<array of _id's>] } }).forEach(function(doc) {
oldVal = doc.option1
newVal = compute_newVal_from_oldVal(oldVal)
db.collection.update({ "_id" : doc._id }, { "$set" : { "option" : newVal } })
})
The document structure is rather schematic, so I omitted putting in actual code to create newVal from oldVal.
Since it is an embedded document type you could use push query
db.collectionname.update({_id:"xxx"},{$push:{option1:{"op1":"value1","op2":"value2"}}})
This will create document inside embedded document.Hope it helps

Add a new field point type filed to a collection with value of an existing field

I have a huge mongodb collection with 6 million records. I have two fields (latitude, longitude), and I would like to add a third field to the collection with the type of point (spatial). How to do this in command line or PHP?
It you'd like to add a new field (with the same value) to all documents in a collection, that can be done easily with an update() operation. Consider the following shell example:
db.collection.update(
{},
{ $set: { type: "spatial" }},
{ multi: true }
);
This would set the type field to "spatial" for all documents matching empty criteria {} (i.e. everything), and the multi option allows the update to modify multiple documents instead of just the first document matched (default behavior).
If you only wanted to set the type field where it doesn't already exist, you could tweak the criteria like so:
db.collection.update(
{ type: { $exists: false }},
{ $set: { type: "spatial" }},
{ multi: true }
);
Since you're storing geospatial data, you may want to have a look at MongoDB's 2dsphere indexes. This would allow you to store and index well-formed GeoJSON objects in your document. See this previous answer from a related question for more introductory information on the subject.

Upserts in mongodb when using custom _id values

I need to insert a document if it doesn't exist. I know that the "upsert" option can do that, but I have some particular needs.
First I need to create the document with its _id field only, but only if it doesn't exist already. My _id field is a number generated by me (not an ObjectId). If I use the "upsert" option then I get "Mod on _id not allowed"
db.mycollection.update({ _id: id }, { _id: id }, { upsert: true });
I know that we can't use the _id in a $set.
So, my question is: If there any way to a "create if doesn't exists" atomically in mongodb?
EDIT:
As proposed by #Barrie this works (using nodejs and mongoose):
var newUser = new User({ _id: id });
newUser.save(function (err) {
if (err && err.code === 11000) {
console.log('If duplicate key the user already exists', newTwitterUser);
return;
}
console.log('New user or err', newTwitterUser);
});
But I still wonder if it is the best way to do it.
I had the same problem, but found a better solution for my needs. You can use that same query style if you simply remove the _id attribute from the update object. So if at first you get an error with this:
db.mycollection.update({ _id: id }, {$set: { _id: id, name: 'name' }}, { upsert: true });
instead use this:
db.mycollection.update({ _id: id }, {$set: { name: 'name' }}, { upsert: true });
This is better because it works for both insert and update.
UPDATE: Upsert with _id can be done without $setOnInsert, as explaind by #Barrie above.
The trick is to use $setOnInsert:{_id:1} with upsert, that way the _id is only written to if it's an insert, and never for updates.
Only, there was a bug preventing this from working until v2.6 - I just tried it on 2.4 and it's not working.
The workaround I use is having another ID field with a unique index. Eg. $setOnInsert:{myId:1}.
You can just use insert(). If the document with the _id you specify already exists, the insert() will fail, nothing will be modified - so "create if it doesn't exist" is what it's already doing by default when you use insert() with a user-created _id.
Please note that $setOnInsert don't work easily when you upsert a simple key => value object (not $set or other).
I need to use that (in PHP):
public function update($criteria , $new_object, array $options = array()){
// In 2.6, $setOnInsert with upsert == true work with _id field
if(isset($options['upsert']) && $options['upsert']){
$firstKey = array_keys($new_object)[0];
if(strpos($firstKey, '$')===0){
$new_object['$setOnInsert']['_id'] = $this->getStringId();
}
//Even, we need to check if the object exists
else if($this->findOne($criteria, ['_id'])===null){
//In this case, we need to set the _id
$new_object['_id'] = $this->getStringId();
}
}
return parent::update($criteria, $new_object, $options);
}