I want to create something like a trello board where there are multiple columns and I can drag+drop items across different lists. I want to be able to create the columns dynamically since each user will have different number of lists. For vue-draggable, how can I dynamically create computed values for each list?
My data is in this format (which is a single array of lists):
lists:
[
{
"id":1,
"label":"List1"
"items":[
{ "id": 1, "description: "item1" },
{ "id": 2, "description: "item2" },
]
},
{
"id":2,
"label":"List2",
"items":[
{ "id": 3, "description: "item3" },
]
},
{ ... },
{ ... }
]
But I need computed values for each list so I can drag/drop:
computed: {
list1: {
get() {
return this.list1.items
},
set(value) {
this.list1.items
}
},
list2: {
get() {
return this.list2.items
},
set(value) {
this.list2.items
}
}
}
Is there another approach for this scenario?
Related
I have a column in my database that stores a json string listing the weapons used in each game activity, what I need to be able to do is return the 'values'->'uniqueWeaponKills'->'basic'->'value' when the 'referenceId' key = 1994645182, and 0 if the key,value pair is not in the column.
Example 'weapons' column data
{
"weapons": [
{
"values": {
"uniqueWeaponKills": {
"basic": {
"value": 14,
"displayValue": "14"
}
},
"uniqueWeaponPrecisionKills": {
"basic": {
"value": 0,
"displayValue": "0"
}
},
"uniqueWeaponKillsPrecisionKills": {
"basic": {
"value": 0,
"displayValue": "0%"
}
}
},
"referenceId": 1994645182
},
{
"values": {
"uniqueWeaponKills": {
"basic": {
"value": 2,
"displayValue": "2"
}
},
"uniqueWeaponPrecisionKills": {
"basic": {
"value": 1,
"displayValue": "1"
}
},
"uniqueWeaponKillsPrecisionKills": {
"basic": {
"value": 0.5,
"displayValue": "50%"
}
}
},
"referenceId": 1853180924
}
]
}
Edit 1:
Using the suggestion from Kendle I got to the following query, I haven't seen a way to dynamically look in each of the array elements instead of having to specify the one to look at.
Query
select weapons::json->'weapons'->1->'values'->'uniqueWeaponKills'->'basic'->>'value' as "uniqueWeaponKills",
weapons::json->'weapons'->1->'referenceId' as "weaponId"
from activities
where (weapons::json->'weapons'->1->>'referenceId')::BIGINT = 1687353095;
You could try
SELECT
weapons::json->'values'->> 'uniqueWeaponKills'->>'basic' ->>'value'
FROM table_name
WHERE
weapons::json->'referenceId' = 1994645182;
See also How to parse JSON in postgresql
I think I found the solution I am looking for using json_array_elements()
SELECT obj->'values'->'uniqueWeaponKills'->'basic'->>'value' as "uniqueWeaponKills"
FROM activities a, json_array_elements(a.weapons#>'{weapons}') obj
WHERE (obj->>'referenceId')::BIGINT = 1687353095;
const response = await simpleSurveyResponsesModel
.find({
"metaKey.projectId": 7,
})
.select([
"answers.SEC.text",
"answers.S4.text",
"answers.S3_a.text",
"answers.Urban_City.text"
])
.lean();
Where answers feild is array of objects containing both empty ad non-empty objects.
collection.find() returns the selected objects but also empty objects as well, inside answers array which i dont need.
My Output
{
"_id": "61840979fcbea215bc61ed03",
"answers": [
{},
{},
{
"Urban_City": {
"text": "Hyderabad"
}
},
]
}
Expected Output
{
"_id": "61840979fcbea215bc61ed03",
"answers": [
{
"Urban_City": {
"text": "Hyderabad"
}
},
]
}
the mongodb data like this:
{
"_id": "123dsadasfa454sdsaw",
"hashmap": {
"uuid-12sadsadw5": {
"name": "bob"
},
"uuid-12sadsadwew5": {
"name": "alice"
}
},
"age": 10
}
"hashmap" like java HashMap, the key is uuid like "uuid-12sadsadwew5" and the value is object.
I want to get the data which the name in "hashmap" value is not null. And I use sql :
db.tabl1.find({"hashmap.values.name":{$ne:null}})
but cannot get the right result
You can use this aggregation query:
First use $objectToArray to create an array with values k and v. As we don't know the key (k) we can search by value (v).
Then $unwind array
And $match values where name is not null.
And then regroup and recreate the object using $arrayToObject.
db.collection.aggregate([
{
"$set": {
"hashmap": {
"$objectToArray": "$hashmap"
}
}
},
{
"$unwind": "$hashmap"
},
{
"$match": {
"hashmap.v.name": {
"$ne": null
}
}
},
{
"$group": {
"_id": "$_id",
"hashmap": {
"$push": "$hashmap"
}
}
},
{
"$set": {
"hashmap": {
"$arrayToObject": "$hashmap"
}
}
}
])
Example here
I have a collection "Owners" and I want to return a list of "Owner" matching a filter (any filter), plus the count of "Pet" from the "Pets" collection for that owner, except I don't want the dead pets. (made up example)
I need the returned documents to look exactly like an "Owner" document with the addition of the "petCount" field because I'm using Java Pojos with the Mongo Java driver.
I'm using AWS DocumentDB that does not support $lookup with filters yet. If it did I would use this and I'd be done:
db.Owners.aggregate( [
{ $match: {_id: UUID("b13e733d-2686-4266-a686-d3dae6501887")} },
{ $lookup: { from: 'Pets', as: 'pets', 'let': { ownerId: '$_id' }, pipeline: [ { $match: { $expr: { $ne: ['$state', 'DEAD'] } } } ] } },
{ $addFields: { petCount: { $size: '$pets' } } },
{ $project: { pets: 0 } }
]).pretty()
But since it doesn't this is what I got so far:
db.Owners.aggregate( [
{ $match: {_id: { $in: [ UUID("cbb921f6-50f8-4b0c-833f-934998e5fbff") ] } } },
{ $lookup: { from: 'Pets', localField: '_id', foreignField: 'ownerId', as: 'pets' } },
{ $unwind: { path: '$pets', preserveNullAndEmptyArrays: true } },
{ $match: { 'pets.state': { $ne: 'DEAD' } } },
{ "$group": {
"_id": "$_id",
"doc": { "$first": "$$ROOT" },
"pets": { "$push": "$pets" }
}
},
{ $addFields: { "doc.petCount": { $size: '$pets' } } },
{ $replaceRoot: { "newRoot": "$doc" } },
{ $project: { pets: 0 } }
]).pretty()
This works perfectly, except if an Owner only has "DEAD" pets, then the owner doesn't get returned because all the "document copies" got filtered out by the $match. I'd need the parent document to be returned with petCount = 0 when ALL of them are "DEAD". I cannot figure out how to do this.
Any ideas?
These are the supported operations for DocDB 4.0 https://docs.amazonaws.cn/en_us/documentdb/latest/developerguide/mongo-apis.html
EDIT: update to use $filter as $reduce not supported by aws document DB
You can use $filter to keep only not DEAD pets in the lookup array, then count the size of the remaining array.
Here is the Mongo playground for your reference.
$reduce version
You can use $reduce in your aggregation pipeline to to a conditional sum for the state.
Here is Mongo playground for your reference.
As of January 2022, Amazon DocumentDB added support for $reduce, the solution posted above should work for you.
Reference.
I have this query that provides me the join I want to:
db.summoners.aggregate([
{ "$match": { "nick":"Luispfj" } },
{ "$unwind": "$matches" },
{
"$lookup": {
"from":"matches",
"localField":"matches.gameId",
"foreignField":"gameId",
"as":"fullMatches"
}
},
{ "$unwind": "$fullMatches" },
{
"$group": {
"_id": null,
"matches": { "$push":"$fullMatches" }
}
}
])
But when I run the unwind function the null entries are gone. How do I retrieve them (with their respective "gameId"s, if possible?
Also, is there a way to retrieve only the matches array, instead of it being a subproperty of the "null-id-object" it creates?
$unwind takes an optional field preserveNullAndEmptyArrays which by default is false. If you set it to true, unwind will output the documents that are null. Read more about $unwind
{
"$unwind": {
path: "$fullMatches",
preserveNullAndEmptyArrays: true
}
},