Given the following document structure:
{
"active": "N",
"code": "AL",
"id": 3,
"mcc": "+355",
"name": "Arango",
"participant": [
{
"actor": {
"reference": "Patient/example",
"display": "Peter James Chalmers"
},
"required": "required",
"status": "not booked"
}
]
}
I want to update array of participant[*].status as "not booked" to "booked".
could you tell me the Arango query for update array value.
Here is a solution using an inline projection:
FOR doc IN coll
FILTER doc._key == "<key>"
UPDATE doc WITH {
participant: doc.participant[* RETURN MERGE(CURRENT, {status: "booked"})]
} IN coll
Note that this sets the status to "booked" for all objects in the participant array.
Related
{
"name": "user2",
"avatar": 1,
"email": "example#gmail.com",
"categories": [
{
"cname": "Category 1",
"list": [
{
"status": "pending",
"name": "List Item 1"
},
{
"status": "pending",
"name": "List Item 2"
}
]
}
]
}
I want to update "categories.list.status" = "pending" to "completed". How can I do it? I tried using positional operator($) but it is giving error too many positional operator.
If you are using Mongoose (as your tags suggest), you can just update the value in the document object and then save it
document.list[0].status = "completed";
document.save();
I tried this. It worked.
document.updateOne({_id: id}, {
{
$set: {
"categories.$[].list.$[ele].status" : status
}
},
{
arrayFilters: [{"ele.name" : name}]
}
}
I have this json schema
"header": {
"self": {},
"items": [
{
"_id": "5ec7e61979ec9914ecefc539",
"title": "Test",
"root": "true",
"alignment": "left",
"page": "test",
"translate": "",
"toggle": "",
"icon": "",
"IsActive": 1,
"submenu": [
{
"_id": "5ece913a353a71309084768d",
"title": "Sub Test",
"bullet": "dot",
"page": "test",
"translate": "MENU.TEST1",
"icon": "flaticon-stes-3",
"IsActive": 1
},
{
"_id": "5ece935d79972f0390997179",
"title": "Sub Test",
"bullet": "dot",
"page": "test",
"translate": "MENU.TEST2",
"icon": "flaticon-stes-3",
"IsActive": 1
}
]
}
]
}
// Index based on a previous query
this.db.collection('AssetData').find({"header.items.$.submenu.[0]._id":ObjectID("5ece913a353a71309084768d"));
//Tried with elemMatch
this.db.collection('AssetData').find(
{
"header.items": {
$elemMatch:{
"submenu": {
$elemMatch:{
"_id":ObjectID("5ece913a353a71309084768d")
}
}
}
}
});
And I am wanting to retrieve one of the sub-menu object data based on the _id from the sub-menu, but I'm having trouble retrieving it.
I'm not sure If I could use an index of the second array from another query to obtain the data, or if there's another way that I'm missing like elem match.
I am using MongoDB 3.5.6.
What would be the best way to retrieve this?
// Index based on a previous query
this.db.collection('AssetData').find({"header.items.$.submenu.[0]._id":ObjectID("5ece913a353a71309084768d")});
//Tried with elemMatch
this.db.collection('AssetData').find(
{
"header.items": {
$elemMatch:{
"submenu": {
$elemMatch:{
"_id":ObjectID("5ece913a353a71309084768d")
}
}
}
}
}).exec(function(err, item) {
console.log(item);
// here you can retrieve the stock from item, as you wish
});
hopefully it will help you
I'll explain: I have this function
function (doc) {
if(doc.MovieId == "1721")
emit(doc.Rating, 1);
}
but it return me some document that are not relevant (for example they haven't the Rating field). My document _id is composed of partitionName:id, so I thought to do if(doc.MovieId == "1721" && doc._id.contains("ratings"){...} but it doesn't work.
Is there a way to do this?
-----EDIT 1-----
The docs in the circle are not relevant.
Do you need the schema of the JSON document?
-----EDIT 2-----
the following documents are NOT RELEVANT
1.
{
"_id": "movies : 1721",
"_rev": "1-d7e0e3c8152d6978073d280e0aef7457",
"MovieId": "1721",
"Title": "Titanic (1997)",
"Genres": [
"Drama",
"Romance"
]
}
2.
{
"_id": "tags : 1490",
"_rev": "1-14c20c9cfb3ee1964a298777f80333d5",
"MovieId": "1721",
"UserId": "474",
"Tag": "shipwreck",
"Timestamp": "1138031879"
}
3.
{
"_id": "tags : 2791",
"_rev": "1-e4d6c9573fcdae726a69d5fc6255de27",
"MovieId": "1721",
"UserId": "537",
"Tag": "romance",
"Timestamp": "1424141922"
}
documets like this are RELEVANT:
{
"_id": "ratings : 31662",
"_rev": "1-446665286337faaf51e23e40b527ec2d",
"MovieId": "1721",
"UserId": "219",
"Rating": "0.5",
"Timestamp": "1214043346"
}
Following view should just emit documents whose _id starts with "ratings :":
function (doc) {
var id_prefix = "ratings :";
if(doc._id.substr(0, id_prefix.length) === id_prefix && doc.MovieId == "1721")
emit(doc.Rating, 1);
}
I have a collection which contains some documents as follows,
{
"transactionId": 3432423,
"reviews": [
{
"fromUser": {
"userId": "5236aa1acd6e"
},
"toUser": {
"userId": "0ec8db9544cc"
},
"rating": 4.3,
"comment": ""
},
{
"toUser": {
"userId": "5236aa1acd6e",
"email": "yourlife#gmail.com",
"firstName": "your",
"lastName": "life"
},
"fromUser": {
"userId": "0ec8db9544cc",
"email": "mylife#gmail.com",
"firstName": "my",
"lastName": "life"
},
"rating": 4.3,
"comment": ""
}
]
}
i need to check if subdocument reviews present inside the document. i have used this query,
db.getCollection('_testCollection').find({ "$elemMatch": { "reviews": { "$exists": false } }})
it throws an error saying,
"errmsg" : "unknown top level operator: $elemMatch",
Neither $elemMatch or $exists is what you want to use to determine if reviews is an empty array.
Instead, either compare against []:
db.test.find({reviews: []})
Or use the $size operator:
db.test.find({reviews: {$size: 0}})
From the Docs :-
The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.
db.getCollection('_testCollection').find({ "reviews": {"$elemMatch" : { "$exists": false } }})
$exists :- true or false // depend on requirement
Thanks
In my cloudant database I have objects like these. I'd like to query for objects that based on properties in a nested array.
In the example below, how to I query for all objects where there is a vote with userId=="user1"? The query should return both objects. When I search for userId "user2" it should return the first one as the second object only has vote from user1 and user4.
{
"_id": "1",
"votes": [
{
"userId": "user1",
"comment": ""
},
{
"userId": "user2",
"comment": ""
},
{
"userId": "user3",
"comment": ""
}
]
}
{
"_id": "2",
"votes": [
{
"userId": "user1",
"comment": ""
},
{
"userId": "user4",
"comment": ""
}
]
}
This view will return a list of all votes (where a vote is userId and _id) sorted by userId, to only get user1 use ?key="user1"
function(doc) {
for(i in doc.votes)
emit(doc.votes[i].userId, doc._id);
}
/dbName/_design/foo/_view/bar?key="user1"
{"total_rows":5,"offset":0,"rows":[
{"id":"1","key":"user1","value":"1"},
{"id":"2","key":"user1","value":"2"}
]}