I noticed that DynamoDB can add and remove items from an array but how do you search for an specific item inside an object if you want to update that one specifically?
For example:
In MongoDB you can search for someitem.$.subitem and update that specific item.
Is there a way on how to do this with DynamoDB?
Item: {
someitem: [
{
subitem: "id",
somevalue: "something"
}
]
}
I would say this is basic functionality but seems not easy to find (or even unsupported)
AWS does not permit to modify it in a single update request more info was found in the following answers:
updating-a-json-array-in-aws-dynamodb.
The solution that they propose is to change the schema from array to {}, or to implement a custom functions and iterate through each array and find your new id to update, so to speak to programatically update your json and then insert whole object.
TableName : 'tablename',
Key : { id: id},
ReturnValues : 'ALL_NEW',
UpdateExpression : 'set someitem['+`index`+'].somevalue = :reply_content',
ExpressionAttributeValues : { ':reply_content' : updateddata }
array element edit via array index
Related
I would like to insert a document that looks like this:
{
name: "app1",
theArray: [...unique string elements...]
}
I tried to do an upsert using the query below but it somehow creates an array within an array if the document does not exist. If I use $push when the document does not exist, then the array is created fine. However I need to use $addToSet to maintain array element uniqueness.
Current query:
collection1.upsert({
name: "app1",
}, {
$addToSet: {
theArray: data // data is an array of ip addresses eg. ["1.1.1", "2.2.2.2"] which is not unique
}
});
Executing the above query when there is no existing document in the db creates:
{
name: "app-1",
theArray: [
//another array that contains the actual data.
]
}
Is there a way I can get this behavior with just a single query?
I need to add to a list of items in Mongo so if I have
items:{item: "apple"}
what would I use to add another item in an object instead of changing that initial object? So I can end up with.
items: {item: "apple"},{item:"orange"},{item:"blueberry"}
Can I use findOneAndUpdate? Or will this over-write the original data. I am having a hard time finding the distinction in the documents.
In closing, what method is used for updating and overwriting and what is used for appending to objects and arrays?
You can use the $addToSet operator.
For example:
db.yourCollection.update(
{ _id: 1 },
{ $addToSet: { items: {item : "orange" } } }
)
The code abode will add the item : "orange" to the items list of the document with id=1
Following is the sample document (userdetails) in couchbase.
{ "friends":[
{
"company":"microsoft",
"firstname":"criss",
"lastname":"angel"
},
{
"company":"google",
"firstname":"captain",
"lastname":null
} ] }
based on the company name, i want to remove the respective json document from the array.
n1ql query
update default use keys "userdetails" set friends=array_remove(friends,a) for a in friends when a.company="google" end returning friends
Im not able to remove the json data using the above query.
This query works properly, if we have empty string ( "lastname" : " ") rather than null value.
So, how to remove, if any of the parameter value is "null"
You can replace the whole friends array as follows:
UPDATE default
USE KEYS "userdetails"
SET friends = ARRAY a FOR a IN friends WHEN a.company <> "google" END
RETURNING friends;
How to query all documents present inside an Array which itself is present in a MongoDB collection under Node.js.
For example: I have a DB with a structure:
{
"name1":[{"height":"5.5"},
{"weight":"57"}],
"name2":[{"height":"6.1"},
{"weight":"74"}]
}
What query should I make to get all the documents( i.e. height, weight) of the array "name1"
Output should be :
{
{ "height":"5.5"}
{"weight":"57"}
}
My suggestion would be to reorganise the collection so each document has a key of name and key of physicalattributes e.g.
{
'name' : 'name1',
'physAttr': ['height': heightvalue,
'weight': weightvalue]
}
then suppose you wanted to find all documents with height 5.5, the query would be trivial
db.collection.find({ 'physAttr.height': 5.5 })
As described in the question each document in the collection has a different schema from the others (different name key for every document) making query operations difficult.
I have this collection:
{_id, list}
I have the elements in the list, of which each has an _id of its own, but I can't figure out how to update all the elements in the list. I do know that all collections have unique elements in the list, including amongst themselves. And I have the elements with updated properties :|
Hope I explained it clearly.
I hope I've got your problem right.
Suppose, you want to update given sub-document in the list by its _id, but you don't know its parent's _id.
To make an update operation you may use the following command:
db.collection.update({
'list._id': subdoc_id
},{
$set: {
'list.$.field1': value1,
'list.$.field2': value2,
'list.$.field3': value3
}
})
To replace an old sub-document with the new one (equivalent of save):
db.collection.update({
'list._id': subdoc._id
},{
$set: {
'list.$': subdoc
}
})
But you should be aware that this operation will completely erase the old sub-document and replace it with the new one. So, any field not listed in subdoc will be deleted.
{'list._id': subdoc_id} query will find you a parent document containing the given sub-document and 'list.$' selector will point to the queried sub-document.
Update: If you don't know in wich particular list the given sub-document is stored, you'll need to run an update operation on each:
['list1', 'list2', 'nested.list'].forEach(function (list) {
var query = {};
query[list+'._id'] = subdoc._id;
var update = { $set: {} };
update.$set[list+'.$'] = subdoc;
db.collection.update(query, update);
})
An update will be performed on each list where the given sub-document is present.