Pull a full object from one value - javascript

I'm working on a ticket system, and I want to make a close ticket system, that will remove the ticket from the user, so I have my mongo array looking like this
{
"tickets": [{
"id": "cxZgqey2",
"subject": "fdgfdgdfgfdgfd",
"message": "gfdgfdgfdgdfg",
"ownerEmail": "soenmtherhg#gmail.com"
},
{
"id": "r4r-CnIC",
"subject": "dfdsfdsfsdfdsfdsf",
"message": "dsfdsfdfsdfdsfdsfdsfdsf",
"ownerEmail": "soenmtherhg#gmail.com"
}
]}
and I wanted to remove the entire object using only the id, how would I do this? (Using the npm module (not mongoose))

You can use the filter function in arrays and delete it passing the relevant id for that and also you can check loadash.

Related

For Prisma Client join queries is it possible to move deeply nested fields to top level of result?

Does Prisma have the ability to move nested fields from another table join to the top level of the result, like a flattened view? I want to put the result JSON into a frontend table without digging through nested objects and building another object.
For example I want to replicate this behavior where I can pick and choose the columns from different tables (columns from User, and School). Currently I use a raw query with a similar SQL, however I wonder if it's possible with using only the Prisma API:
SELECT
u.id
, u.email
, s.school_name
FROM "User" AS u
JOIN "UserSchool" AS us ON us.user_id = u.id
JOIN "School" AS s ON s.id = us.school_id
id | email | school_name
123| student1#email.com | mount high
I want JSON that looks like this:
{
"id": "1",
"email": "student1#email.com",
"school_name": "mount high",
}
If I did this in Prisma, I would need to go into several levels of nested objects to get the same column name on another table for e.g. user[user_school][schoo][school_name]. This requires extra work to loop through all my results, extract from the nested object, and build another object. This example isn't too bad, but I have more joins and deeply nested objects for my actual problem (lots of association/lookup tables). I've experimented with the select and include for my joins, but they are structured with the nested JSON.
users = await prisma.user.findMany({
include: {
user_school: {
include: {
school: true,
},
},
},
{
"id": "1",
"email": "student1#email.com",
"user_school": [
{
"id": 1,
"user_id": "1",
"school_id": "1",
"school": {
"id": 1,
"school_name": "mountain high",
}
}
],
}
It's not possible to flatten out the results as of now.
The only way to achieve this would be by using rawQuery as you mentioned.
However, there is an existing Feature Request which discusses Flattening out the results by providing an option of flatten:true. If you could mention your use case there and add a comment it would help Prisma's Product and engineering team in prioritising it.

Adding and removing items from a cart in Dialogflow

I am prototyping a Food Ordering app using Dialogflow(Chatbot maker) and got stuck with this problem. Technically, I want to persist the gathered data from an Intent after the user decided to "add more items to their order" and satisfies all the required parameters, which are, (itemName, quantity, [variants], [sauceType], ...).
The chat bot should be able to handle a request which consist of multiple items with their corresponding quantities but I am not sure if it's possible to model a data wherein it consist an array of Entities so, my first thought was to use a persistent Fulfillment using session-based Webhook with our custom Web Service, like for example: foodorder/api/order/123/items/add and 123 being the Session Id. But this approach requires more work and the generated model can be difficult to translate in Dialoflow Console.
The second solution comes into my mind, is to leverage the Intent property called Action and Parameters where we mark the Entity as List, but using this approach, the quantity doesn't get attached to the item itself.
My question is, how can I be able to model a data using Dialogflow that resembles something like below:
{
"givenName": "Dummy User",
"order": [
{
"itemName": "Burger",
"quantity": 2
},
{
"itemName": "6 piece Chicken Nuggets",
"quantity": 1,
"sauceType": "Tangy Barbeque"
},
{
"itemName": "Coke",
"quantity": 1,
"size": "Small"
}
]
}
Turns out what I was looking for was Composite Entities and mark it as a list.
The detailed answer can be found on this link:
https://stackoverflow.com/a/47166123/2304737

Get all doc data from Cloudant as opposed to id, key, value

Trying to query against my db to get all docs with all info. The db.list functionality gets the overview of the docs but does not return all the data for the docs. Currently have to get the high level data then loop through the rows and query each individual doc. There must be a better way...
Is there a way to get all docs with the full set of info for each doc?
getting:
{
"id": "0014ee0d-7551-4639-85ef-778f74365d05",
"key": "0014ee0d-7551-4639-85ef-778f74365d05",
"value": {
"rev": "59-4f01f89e12c488ba5b8aba4643982c45"
}
}
want:
{
"_id": "14fb92ad75b8694c05b98d89de6e9b2d",
"_rev": "1-6067c00b37a18ad8bab6744d258e6439",
"offeringId": "ae0146696d1d3a90fe400cc55a97a60e",
"timestamp": 1464165870848,
"srcUrl": "",
"score": 9,
...
}
The repository you linked to for nano looks like an outdated mirror. The official repository includes documentation for db.list which also includes a params object. I would double-check your version of nano, but I would guess you already have a more recent version.
You can simply add { include_docs: true } as the first argument to db.list and alongside id, key and value, you'll get a doc property that has the entire document.

MongoDb: Find the generated id of the inserted child on parent save

If I have a document representing a post with comments that looks like this:
{
"_id": "579a2a71f7b5455c28a7abcb",
"title": "post 1",
"link": "www.link1.com",
"__v": 0,
"comments": [
{
"author": "Andy",
"body": "Wish I had thought of that",
"_id": "579a2a71f7b5455c28a7abcd",
"upvotes": 0
},
{
"author": "Jim",
"body": "Just a comment",
"_id": "579a2a71f7b5455c28a7abcc",
"upvotes": 0
}
],
"upvotes": 5
}
In the calling (javascript) code, I add a new comment, by pushing to the post.comments array, then save the post using .save with a callback. In the save callback, I want to get the generated _id of the new comment I just saved. How do I do this?
I've got the parent post document in the callback, of course, but that's not useful as I can't tell which comment was just inserted.
Is there another document method or an alternate form of the .save callback to deal with my situation?
Or do I have to just follow what I'd usually do and generate a unique id on the comment myself before the save?
EDITED: I'm using Mongoose, sorry, forgot to say!
You did not specifically tell, but I assume you use Mongoose because standard MongoDB will not add an _id property to subdocuments.
As mentioned in the Mongoose documentation regarding adding sub-documents, you can use the following code example:
var Parent = mongoose.model('Parent');
var parent = new Parent;
// create a comment
parent.children.push({ name: 'Liesl' });
var subdoc = parent.children[0];
console.log(subdoc) // { _id: '501d86090d371bab2c0341c5', name: 'Liesl' }
subdoc.isNew; // true
parent.save(function (err) {
if (err) return handleError(err)
console.log('Success!');
});
Instead of parent.children[0] you have to use parent.children[parent.children.length - 1] to access the inserted element, though.
I'd assume the item you pushed on the array would be the last one, but that wouldn't work in a multi user system.
Also you could make a comparison against the author and comment fields, though this seems like a lot of trouble, and with just the author and the comment text, you might not be assured a match.
Finally, you could also create the object id and assign it to the comment, then save it. You do that like this:
var mongoose = require('mongoose');
var id = mongoose.Types.ObjectId();
That's what I would do.

Retrieve the value from a JSONP formatted data object

Just when I think I've got he hang of identifying an element in an object, I run into a scenario that I cannot seem to get the value I want.
This part works and the data returned is correct: I have a map and when I attempt to identify a building on the map, I receive the following json object (this has been shortened for readability but in real life, its properly formatted): The function MapClick(queryResults) is called when the map is clicked.
dojo.io.script.jsonp_dojoIoScript19._jsonpCallback({
"results": [
{
"layerId": 5,
"layerName": "Building",
"value": "Name of item clicked",
"displayFieldName": "name",
"attributes": {
"ID": "123",
"name": "Name of item clicked",
"Variable1": "Some bit of information",
"Variable2": "Some other bit of information",
...
...
All I'm trying to do is return either the results[0].value OR results[0].attributes.name which in this example should bring back "Name of item clicked". The layerId, layerName, value, and displayFieldName are the "common most accessed data" so they are returned but the same information is also found within the attributes.
I've tried console.log(results[1].attributes.name); and console.log(results) with no success.
Turns out the name of the function handling the MapClicked is queryResults was needed so the correct answer is: queryResults[0].value and when you see open brackets [, you can be sure you will need [ some number ] (e.g. queryResults[0].value or queryResults[99].someothervariable. Or at least I think this is a correct statement.

Categories

Resources