How to remove __table from Tabel Knex.js select query - node.js

How to remove __table from Tabel Knex.js select query.
The select query returns all data i queried +__table but how can i remove __table.
My select query
table('TestSeriesModes').select('ID', 'Name', 'IsActive', 'IsPhysicalInventory', 'Code').orderBy('ID', 'asc').all()
Returning data
{
"__table": "TestSeriesModes",
"ID": 1,
"Name": "Online",
"IsActive": true,
"IsPhysicalInventory": true,
"Code": "ON"
},
{
"__table": "TestSeriesModes",
"ID": 2,
"Name": "Calling Tablet",
"IsActive": true,
"IsPhysicalInventory": true,
"Code": "CALTAB"
}

Knex doesn't even have that .all method (at least officially). Looks like you are not using knex directly here. Knex never adds anything like you have described (key called __table) to your resulting rows. Complete reproduction case would be useful, because now the post does not have enough code shown to be able to tell what is wrong with it.
To answer the query, you can for example use knex directly to get results without that key. Maybe you are using some ORM, which adds it or some other library.
Other way to drop it would be to filter it out from results afterwards for example by doing:
const filteredResults = results.map(res => {
delete res.__table;
});
Or to use some library like ramda / lodash to filter extra keys from resulting objects.

Related

Sequelize "raw = true" changes json model attribute name with dot

I have Content model and it has many sub ContentImage item, like this;
const content = await db.Content.findOne({
where: {
permalink: req.params.permalink
},
include: [{
model: db.ContentImages
}]
raw: true
});
As you know raw:true covert SequelizeInstance to Object model. I have some problem in this point.
If I use raw:true, json model show me like this;
{
"id": 4706,
"name": "Content Title",
"content": "Content detail",
"t_content_images.id": 7633,
"t_content_images.content_id": 4706,
"t_content_images.image": "content-image-1.jpg",
"t_content_images.order_no": 1
}
Because of expressjs, I need like this model instead of SequelizeInstance;
{
"id": 4706,
"name": "Content Title",
"content": "Content detail",
"t_content_images": {
"id": 7633,
"content_id": 4706,
"image": "content-image-1.jpg",
"order_no": 1
}
}
Another problem, I have multiple content image and if I use like above first sample, it returns me just first content image.
You are thinking about it a bit backwards - when you use raw: true it doesn't convert it from a JSON object to a Model Instance.
If you think of how SQL results are structured, they always come back flat. This means that for joins where you have one base record linked to multiple children (Content -< ContentImages in this case) then the SQL results will repeat the info for the base record for each of the children. Sequelize will parse this into a JSON object, which is what you are seeing in the first example in your question. If you leave out raw: true then it will take it a step further and parse it into an instance of your model. You can then call Model.toJSON() to get a JSON representation of the parsed object.
Given the above, if you are fetching lots of children then it can be more efficient to get the data into two queries instead of one so you don't have to send the repeating data to the client.

Why is searching arrays using cts different than searching arrays using node api?

I have a question similar to this question:
Marklogic (Nodejs API) - Search documents that match 2 (or more) conditions in object array attribute
I have the following document:
{
"address": [
{ "type": "mailing",
"street": "1001 Main Street",
"city": "Springfield",
"state": "MO"
},
{ "type": "location",
"street": "989 First Street",
"city": "Johnstone",
"state": "WY"
}
]
}
When I run the following code within query console, it correctly does not return the document:
'use strict';
const queryText =
cts.jsonPropertyScopeQuery("address", cts.andQuery([
cts.jsonPropertyWordQuery("city", "Johnstone"),
cts.jsonPropertyWordQuery("state", "MO")
]));
cts.search(queryText);
When I run this code in Node.js, it does return the document because it appears to combine all of the array nodes when evaluating.
const queryText =
qb.scope(qb.property("address"), qb.and(
qb.word("city","Johnstone"),
qb.word("state","MO")
));
const query = qb.where(queryText);
Is there a way I can get cts functionality using the Node API? I would prefer to use the Node API versus using invoke on a server side javascript query.
By default, the SJS searches run filtered, which will remove any false positive results. You can toggle that behavior by adding explicit options to the SJS search:
cts.search(queryText, "unfiltered");
By default, the Node.js queries are run unfiltered, which means that you may encounter false positive results.
In order to get your Node.js search running filtered, add the filtered search option to your query:
const query = qb.where(queryText)
.withOptions({search:['filtered']});

SELECT single field from embedded array in azure documentDB

I have a documentDB collection that looks like this sample:
{
"data1": "hello",
"data2": [
{
"key": "key1",
"value": "value1"
},
{
"key": "key2",
"value": "value2"
}
}
In reality the data has a lot of other fields and the embedded array has some fields where the data is quite large. I need to query the data and I care about the small "key" field in the data2 array but I do not need the large "value". I am finding returning all the value data is causing performance problems, but if I exclude the array data from the SELECT all together it is fast (so the data size is the issue).
I cannot figure out a way to return only the "key" but exclude the "value" in the embedded array.
I basically want SELECT r.data1, r.data2.key and to have it return as:
{
"data1": "hello",
"data2": [
{
"key": "key1"
},
{
"key": "key2"
}
}
but it doesn't seem possible to SELECT r.data2.key because it is in an array
A JOIN will cause it to return a copy of each document for each "data2" array element, which does not work for me. My only other option would be to migrate the data and put the data I want into its own array so I can select the whole object.
Is this possible some how that I have not been able to figure out?
Mike,
As you have surmised, this is not possible without a custom UDF until DocumentDB supports sub-queries. If you would like to go down that route, see the following answer for an example of how the UDF may have to look:
DocumentDB Sub Query
Good luck!

mongo update query to ignore few fields in update operation

I am using Nodejs based mongoskin driver for mongo database operation. I want to update my document however don't want to update few fields. Following are more details.
Request for add:
{
"name": "Theme Name",
"description": "Theme Description",
"createdByUserId": "53651221b25521601a5c9530",
}
Request for update:
{
"_id":"53555ef203dabf282b750a81"
"name": "Theme Name",
"categoryId": "53555ef203dabf282b750a81",
"description": "Theme Description",
"createdByUserId": "53651221b25521601a5c9530",
"updatedByUserId": "5675561b25521601a5c9530",
"dateCreated": ISODate("2014-05-19T19:47:26.603Z"),
"dateUpdated": ISODate("2014-05-19T19:49:28.203Z"),
}
I want to ignore following field send by client.
1. createdByUserId
2. dateCreated
For time being I am taking following approach in update operation:
1. Read collection for given _id
2. Read these above two fields from database and update the request and then perform database update operation
Looking help for clean approach.
Your request for update actually does the following: it replaces everything in the document with the values provided by the request (except for the "_id" of course, which is immutable). What you want is what is called a "partial update" in mongosphere. Please have a look into the $set operator. So what you would do is something like:
db.yourcollection.update({"_id":"53555ef203dabf282b750a81"},
{$set:
{
"categoryId":"53555ef203dabf282b750a81",
"updatedByUserId":"5675561b25521601a5c9530",
"dateUpdated":ISODate("2014-05-19T19:49:28.203Z")
}
}
)
As far as I know there is now way of sending a complete document to a mongo[s|d] and tell it to only skip certain fields.

join within a same collection using mongoose

Pardon my ignorance if it is a very basic question.
ABC collection in MongoDB has the following schema.
{
"metadata": {
"store": 5051,
"catg": "XYZ",
},
"category": {
"name": "XYZ",
"id": "CL141778",
}
}
I need to query (where "metadata.catg" == "category.name")
What is the best way to do it without using mongoose dbref ?
In MongoDB you can not compare two fields values with each other in a query. What I expect is that you need to redesign your schema. You need to look at how you query (and update) as supposed to thinking MongoDB is yet another RDBMs.
Show us the real schema, some example documents and a few query types that you need to run and I can see if I can help.

Resources