I have collection as below
"_id" : "PS8720_18",
"AlertID" : "18",
"Status" : "ACTIVE",
"triggerHistory" : [
{"triggerId" : "1535081507421"},
{"triggerId" : "1535105196735"},
{"triggerId" : "1535341330335"},
{"triggerId" : "1535364578821"}
]
I want to delete all element and just want keep last two entries in the array. Each document has different no of elements in array. How do I achieve this?
Please Check with the following Query
db.getCollection('youtablename').update({}, {
$push: {
triggerHistory: {
$each: [ ],
$slice: -2
}
}},{multi:true})
Hope it Helps !!
Related
I have a Mongo database with multiple documents and they all contain 2 Items for in store and online locations. The difference will be that attributes within the items object may differ. I am trying to capture all documents that have differing attributes between the object items with the array.
I've tried using $expr and comparing the position of the elements such as "Items.0.Attributes" == "Items.1.Attributes" but had no luck.
{
"ID" : "123456789",
"Items" : [
{
"ItemDept" : "softLines",
"ProductId" : {
"Name" : "shirts",
"_id" : "12345Shirts"
},
"Attributes" : [ "blue","small","mens"],
"Season" : "Summer"
"Location":"online"
}
,
{
"ItemDept" : "softlines",
"ProductId" : {
"Name" : "shirts",
"_id" : "12345Shirts")
},
"Attributes" : [ "blue","small","women"],
"Season" : "Summer"
"Location":"stores"
}
]
}
If I understand what you want to find/output, here's one way you could do it.
db.collection.find({
"$expr": {
"$not": {
"$setEquals": [
{"$first": "$Items.Attributes"},
{"$last": "$Items.Attributes"}
]
}
}
})
Try it on mongoplayground.net.
{
"_id" : ObjectId("5fa27539e2b1a10d0fdb82ba"),
"extra_fields" : [
{
"tag" : "alias",
"value" : "thealias"
},
{
"tag" : "name",
"value" : "nobody"
},
]
}
This is the object I'm referring to, for example, I need to query all the objects which have a tag="alias" in the extra_fields array of each object. how can I achieve this with MongoDB? Any help would be highly appreciated.
UPDATE: Please check this,
{
"_id" : ObjectId("5fa1b8ee30caf0655a64104a"),
"status" : true,
"extra_fields" : [
{
"tag" : "alias",
"value" : "hes"
},
{
"linked_collection_id" : "5fa1946a3ef5864410728f1f",
"linked_master_id" : [
"5fa19884dae0ba587c891d9a"
]
},
{
"linked_collection_id" : "5f7d68861d5a43b422b12c6d",
"linked_master_id" : [
"5f7d6c3b629ab057b67fa789"
]
}
]
}
I need to know how can I retrieve objects which has linked_collection_id='abc' AND linked_master_id contains 'xyz'. Thanks
You only need "extra_fields.tag": "alias" to compare the field and the result.
The query is quite simple:
db.collection.find({
"extra_fields.tag": "alias"
})
In this way, every document with, at least, one 'tag' field with value 'alias' will be returned.
Example here
Edit:
To find by multiple fields you can use this:
db.collection.find({
"extra_fields.linked_collection_id": "abc",
"extra_fields.linked_master_id": "xyz"
})
I'm working with mongo 3.4.13 and I want to add an element in this document:
{
"_id" : ObjectId("5981d38cf43047f3235febbc"),
"code" : "A0001",
"subItems": [
{
"_id" : ObjectId("5981d38cf43047f3235febbc"),
"code" : "A1001",
"subItems": []
},
{
"_id" : ObjectId("5981d38cf43047f3235febbc"),
"code" : "A2001",
"subItems": [
{
"_id" : ObjectId("5981d38cf43047f3235febbc"),
"code" : "A2101",
"subItems": [
//I wanna add an element here
]
}
]
}
]
}
But I have no idea how to do it, any idea?
According to description as mentioned into above question I assume that you need to add an element into subItems array field belonging to BSON Document as mentioned above.
$addToSet operator is used to append a value into an array field as a part of update operation only if value does not exist into an array thereby ensuring uniqueness of values into an array field.
Hi I'm trying to remove multiple objects from an array that looks like this.
{
"_id" : ObjectId("5a7da1bda21d5f3e8cf005b3"),
"owner" : "1",
"group_name" : "PAASCU Board",
"group_members" : [
{
"faculty_name" : "Cheska Dela Rosa",
"faculty_number" : 2,
"_id" : ObjectId("5a7da1bda21d5f3e8cf005b5")
},
{
"faculty_name" : "Earl Sempio",
"faculty_number" : 7323,
"_id" : ObjectId("5a7da1bda21d5f3e8cf005b4")
},
{
"faculty_number" : 203,
"faculty_name" : "Sample",
"_id" : ObjectId("5a7dbf7952bd150a94d83958")
},
{
"faculty_number" : 8025,
"faculty_name" : "Sample Postman",
"_id" : ObjectId("5a7dc64a1cf5dd3d50167d53")
}
],
"__v" : 0 }
It works when I remove a single object using the $pull with this code.
db.getCollection('groups').update({_id: ObjectId("5a7da1bda21d5f3e8cf005b3")}, {$pull: {"group_members": {"faculty_number":8025}}})
But what if I want to remove multiple objects with different faculty_number? I tried using the $each method just like how I add multiple objects in the array but it doesn't work well.
Use $in operator to pass the list of faculty values to remove documents from embedded array. More here
Try
db.groups.update(
{"_id": ObjectId("5a7da1bda21d5f3e8cf005b3")},
{"$pull":{"group_members":{"faculty_number":{$in:[8025,7323]}}}}
)
I have a requirement to update an array of multiple elements.My collections is in the following way
{
"_id" : ObjectId("53e87e239ae974e6a0a81004"),
"name" : "mulagala",
"notifications" : [
{
"name" : "apple",
"status" : 0
},
{
"name" : "microsoft",
"status" : 0
},
{
"name" : "android",
"status" : 0
}
]
}
now i want to change the every status element of the array should be changed to 1, ie.status:1 with a single query.
I tried in the following way
db.mystatus.update({'notifications.status':0},{$set:{'notifications.$.status':1}},false,true)
But the first record only updating, what to do.Any help would be appriciated!
Have you tried updating the elements of the array using the $ operator for arrays? Currently it updates only one element as the index is coded to 0.