How would I query the the followoing:
get the value of id '120154' for document '123' (result should be 32')
Collection Name:'Collection'
{
"_id" : "123",
"Data" : {
"_v" : [
[
120154,
32
],
[
120156,
"0.940515536000"
],
[
120157,
ISODate("2013-12-26T00:00:00Z")
],
[
120158,
"ABX-HE-AA 06-1"
],
[
120159,
"0A08AG9A4"
]
]}}
db.collection.find({"_id" : "123"},{"Data._v.$1" : {"$in" : {120154}}}). I think this should work when u know the _id for the document.
I had the same problem and so far I'm unable to find the solution because mongodb queries always return a whole document but you can project fields but that won't return you 32. So when you query {_id : 123} AND {no matter what else}, It will return you the matching whole documents not inner arrays.and I might be wrong but that's what I've experienced
Related
{
"_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 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 !!
I'm new to node.js and admittedly I'm probably trying to learn the hard way but I use the following code to parse json looking for the id when the corresponding name contains the uuid. The uuid gets passed from a different function.
var arrFound = Object.values(json).filter(function(obj) {
return obj.id && obj.name.includes(uuid);
}).map(function(obj) {
return obj.id;
});
console.log (arrFound); // array of matched id values
I expect this to return something like [ 'local-1517085058363' ].
Instead I get this:
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
[ 'local-1517085058363' ]
The json comes from the Spark HistoryServer API(http://127.0.0.1:18080/api/v1/applications), although it's just JSON so shouldn't matter where it comes from. Here is a sample:
[{
"id" : "local-1517085058363",
"name" : "TeraGen (5MB) 6e722700-0397-11e8-ae84-b54a3ebb27aa",
"attempts" : [ {
"startTime" : "2018-01-27T19:22:37.513GMT",
"endTime" : "2018-01-27T19:22:43.253GMT",
"lastUpdated" : "2018-01-27T19:22:43.000GMT",
"duration" : 5740,
"sparkUser" : "paulcarron",
"completed" : true,
"endTimeEpoch" : 1517080963253,
"startTimeEpoch" : 1517080957513,
"lastUpdatedEpoch" : 1517080963000
} ]
} ]
Also, I'm running my code on Gulp. I'm not sure if that will make any difference.
How do I fix this so I only return one single id?
There was multiple arrays so that explains where they came from. I used a reduce() to get the object I was looking for.
This is my collection
{
"_id" : ...,
"name" : "sport",
}
{
"_id" : ...,
"name" : "art",
}
{
"_id" : ...,
"name" : "cars",
}
This is an array I have got ["cars","sport"]
I just want to make sure that I have got cars and sport in my collection, if not(or just one doesn't exist) I would like to receive nothing.
I'm looking for a array-friendly like query like $in but and mode.
Is this what you are looking ..??
db.colllection.find( {$and: [ {"name": {$in: ["cars"]}},
{"name": {$in: ["sports"]}}
]
})
following query should help you achieve this
//change test with your collection name
db.getCollection('test').aggregate(
{
$group: {
_id: "collectionOfNames",
"names": {$addToSet: "$name"}
}
},
{
$match: {"names": {$all: ["cars", "sport"]}}
}
)
We use $group to create an array "names" of the property name across all documents.
Then using $match and $all we check to make sure that the array "names" contains all of the elements from our query array.
Is this what you are looking for?
I need some help. I am trying to find an array in another array.
Records:
{
"_id" : ObjectId("56b7e6966213e8d142ef55b7"),
"users" : [
ObjectId("56b0e547a838b5a45b4d0100"), // 0
ObjectId("56a7e16e37e5adf32b97cc3d") // 1
]
},
{
"_id" : ObjectId("56b7e6966213e8d142ef55b7"),
"users" : [
ObjectId("56b0e547a838b5a45b4d0100"),
ObjectId("56a7e16e37e5adf32b97cc3d"),
ObjectId("56b7e6b96213e8d142ef55b8")
]
}
I'm trying to find only first record "_id" : ObjectId("56b7e6966213e8d142ef55b7"),
I'm using query:
db.collection.find({"users" : { $in: [
ObjectId("56b0e547a838b5a45b4d0100"),
ObjectId("56a7e16e37e5adf32b97cc3d")
]}})
or
db.collection.find({ $and: [{
"users": ObjectId("56b0e547a838b5a45b4d0100")
}, {
"users": ObjectId("56a7e16e37e5adf32b97cc3d")
}]})
Response gives me two records.
This request gives me the correct response:
db.collection.find({"users" : [
ObjectId("56b0e547a838b5a45b4d0100"), // 0
ObjectId("56a7e16e37e5adf32b97cc3d") // 1
]})
but if record has an array like this:
{
"_id" : ObjectId("56b7e6966213e8d142ef55b7"),
"users" : [
ObjectId("56a7e16e37e5adf32b97cc3d"), // 1
ObjectId("56b0e547a838b5a45b4d0100") // 0
]
}
it doesn't work
$all, $eq, $in also doesn't work
db.rooms.find({
users: {
$all: [
ObjectId("56a7e16e37e5adf32b97cc3d"),
ObjectId("56b0e547a838b5a45b4d0100")
]
}
})
I need to find rows where [1,2] == [1,2] or [1,2] == [2,1]
not where [1,2,3] == [1,2]
I will appreciate if somebody can help me.
If all you expect is the resulting document with "only" those two array entries, then the combination of conditions you want is $all and $size:
db.rooms.find({
"users": {
"$all": [
ObjectId("56b0e547a838b5a45b4d0100"),
ObjectId("56a7e16e37e5adf32b97cc3d")
],
"$size": 2
}
})
Where $all is basically an $and with shorter syntax, and the $size retricts that since the list as n entries, then you only want to match something of that particular length/size.
This is better than specifying the array directly, since it will match in either order, as opposed to this statement that would not match considering the order is different and therefore not an "exact" match:
db.rooms.find({
"users": [
ObjectId("56a7e16e37e5adf32b97cc3d"),
ObjectId("56b0e547a838b5a45b4d0100")
]
})
So logically the array contains "all" the specified entries, and is the same "size" as the input and no different.