How to query json object array in SQL Server - sql-server

I want to query the url in a field value like this:
["{\"Success\":null,\"Fail\":null,\"Url\":\"http://www.baidu.com\",\"Method\":\"Post\",\"Data\":\"\",\"ContentType\":\"application/json\",\"Timeout\":120000,\"DelayFromMinutes\":15,\"Cron\":\"33 10 3/6 * * ?\",\"JobName\":\"Test#asyncOrder\",\"QueueName\":\"recurring\",\"AgentClass\":null,\"SendSuccess\":false,\"SendFail\":true,\"Mail\":\"xxxxxx#qq.com\",\"EnableRetry\":true,\"RetryDelaysInSeconds\":null,\"RetryTimes\":0,\"BasicUserName\":null,\"BasicPassword\":null,\"Headers\":{},\"CallbackEL\":null,\"TimeZone\":null,\"DingTalk\":null}","\"Test#asyncOrder\"","\"recurring\"","true",null]
What should I do?

I think I need some further clarification on this.
Are you trying to pass the entire json object into the query as an array of key-value pairs?
If so, do you want that pass to be all one string--commas, colons and everything?
OR are you passing the value for each key into its own parameter in the query?
In any event, I would likely want to utilize STRING_ESCAPE:
... WHERE Thing = STRING_ESCAPE(#Thing, 'json') ...

Related

How to filter based on the last element in ArrayField in django

I'm using postgresql database which allows having an array datatype, in addition django provides PostgreSQL specific model fields for that.
My question is how can I filter objects based on the last element of the array?
class Example(models.Model):
tags = ArrayField(models.CharField(...))
example = Example.objects.create(tags=['tag1', 'tag2', 'tag3']
example_tag3 = Example.objects.filter(tags__2='tag3')
I want to filter but don't know what is the size of the tags. Is there any dynamic filtering something like:
example_tag3 = Example.objects.filter(tags__last='tag3')
I don't think there is a way to do that without "killing the performance" other than using raw SQL (see this). But you should avoid doing things like this, from the doc:
Tip: Arrays are not sets; searching for specific array elements can be
a sign of database misdesign. Consider using a separate table with a
row for each item that would be an array element. This will be easier
to search, and is likely to scale better for a large number of
elements.
Adding to the above answer and comment, if changing the table structure isn't an option, you may filter your query based on the first element in an array by using field__0:
example_tag3 = Example.objects.filter(tags__0='tag1')
However, I don't see a way to access the last element directly in the documentation.

how to append item to an array value with Sequelize + PostgreSQL

I have a posgresql database, table has a column which is an array: Sequelize.ARRAY(Sequelize.BIGINT).
What is the right way to append a new item to the array?
I am new to posgresql, sequelize and nodejs. May be it is a trivial question.
From reading around I think I know how to use Promise.all to read all rows, append, and update back.
The question, isn't there any useful shortcut.
PostreSQL documentation mentions a function array_append(anyarray, anyelement).
Sequelize documentation offers a function fn, which Creates an object representing a database function, but only seems to be working in where and order parts
Any way to combine those for an append-like update?
Array Datatype has many methods like append, concat etc. which you can see here. I will give an example with array_append function.
Room is a sequelize model and job_ids is a column in this model with datatype as Array of integer. sequelize is an instant of Sequelize class.
Room.update(
{'job_ids': sequelize.fn('array_append', sequelize.col('job_ids'), new_jobId)},
{'where': {'id': roomId}}
);
Assuming default value of this column is an empty array else it may throw an error.

Laravel show records as flat array or single record

I have 2 column in my table setting
with the following values
KEY VALUE
company ABC
phone 14344
address Somerset City
I need to display this like a single record or a flatten
array in the view/blade page
something like
{{$sett->company}}
{{$sett->phone}}
or an array with lookup
{{$myarray('company')}}
{{$myarray('phone')}}
The idea is if I add another settings like contact us email address
for my website I don't want to add another column.
I know this is achievable in controller by creating different variable
and executing different query but I'm kind of looking for some options here.
Thanks for the help really appreciated.
You can use $settings->pluck('value', 'key') to get your result. Read more here: https://laravel.com/docs/5.4/collections#method-pluck

ordered fixed-length arrays in mongoDB

I'm new to monogDB and trying to design the way I store my data so I can do the kinds of queries I want to. Say I have a document that looks like
{
"foo":["foo1","foo2","foo3"],
"bar":"baz"
}
Where the array "foo" is always of length 3, and the order of the items are meaningful. I would like to be able to make a query that searches for all documents where "foo2" == something. Essentially I want to treat "foo" like any old array and be able to index it in a search, so something like "foo"[1] == something.
Does monogDB support this? Would it be more correct to store my data like,
{
"foo":{
"foo1":"val1",
"foo2":"val2",
"foo3":"val3"
},
"bar":"baz"
}
instead? Thanks.
The schema you have asked about is fine.
To insert at a specific index of array:
Use the $position operator. Read here.
To query at a specific index location:
Use the syntax key.index. As in:
db.users.find({"foo.1":"foo2"})

Compound sort not working in Spring mongodb

I have a requirement where the records will be sorted based on created date first and if created dates are same, we will sort on another field called as ratings.
In my Spring mongo project I am doing the following thing:
Query query = new Query();
query.with(new Sort(Direction.DESC, "crDate")).with(new Sort(Direction.DESC, "ratings"));
For some reasons its only sorting on the first field ie crDate. And if both dates are same, sort by ratings never work.
When i try to check the value of sort object it shows me this:
{"crDate":-1,"ratings":-1}
Another finding is, mongo takes in the following syntax for compound sorts:
db.abc.find({..some criteria..}).sort([["crDate",-1],["ratings",-1]]);
Is this a bug in spring mongodb implementation or I missed something?
Looking at the Spring API Documentation it shows you can specify multiple strings to the sort object you are creating in a list. From you snippet above I would suggest you need to only apply the one sort object that takes the two fields, something like
query.with(new Sort(Direction.DESC, Arrays.asList("crDate", "ratings")));
There was another constructor that took the List of Order objects. Strange but I tried it with that now and it seems to be working.
I am now using a single with clause and passing in a List of Order

Resources