How to query mongo db with a nested model? - json

I have data similar to the below in my mongodb table called Resources.
{
"_id":"testuser",
"_class":"com.Resources",
"allocations":[
{
"contractId":"5083",
"status":"UNKNOWN"
}
]
}
{
"_id":"testuser",
"_class":"com.Resources",
"allocations":[
{
"contractId":"5084",
"status":"Dead"
}
]
}
{
"_id":"testuser2",
"_class":"com.Resources",
"allocations":[
{
"contractId":"5085",
"status":"Live"
}
]
}
I would like to run a query in a shell that returns all contractIDs and its status for each _id, which is my resourceID effectively in the table. The format should be "_id - contractId - status". For example when run with the above data, we should see the below:
testuser - 5083 - UNKNOWN
testuser - 5084 - Dead
testuser2 - 5085 - Live
Any help is appreciated.

Try this
db.getCollection('Collection').find(/*Some Query*/).forEach(function(data){
data.allocations.forEach(function(result){
print(data._id + '-' + result.contractId + '-' + result.status);
})
})

Related

Create nested reponse from sql view with spring boot DTO

I have a view generated form multiple tables, I want to return a nested response from the view like the following.
[
{
"id":"c2bb81dd-6837-4dd7-b903-4c2ec938f78a",
"name":"Olive Oil",
"date":"2023-01-05",
"quantity":180,
"consumption":50,
"demand":0.00,
"max":100.00,
"safety":20.00,
"unit":"L",
"consumedProducts":[
{
"id":"197f88e6-b941-414d-b571-8be6a64ce82f",
"name":"Burger",
},
{
"id":"0010f6d6-71d3-4553-aaf5-96383b3c0c28",
"name":"Sandwich",
}
],
"wastage":{
"preparation":15.25,
"expiration":1
}
}
]
I want to write hibernate query with projection to return this response
The view data.
enter image description here
I tried the following query but it didn't get the right response
select new com.intelmatix.demo.service.dto.IngredientViewDTO( iview.id as id,\n"
+ "iview.name as name,\n"
+ "iview.date as date,\n"
+ "iview.quantity as quantity,\n"
+ "iview.consumption as consumption,\n"
+ "iview.demand as demand,\n"
+ "iview.max as max,\n"
+ "iview.safety as safety,\n"
+ "iview.type as type,\n"
+ "iview.pid as pid,\n"
+ "iview.productname as productname)\n"
+ "from IngredientView iview\n"
+ "group by
iview.id,iview.date,iview.quantity,iview.consumption,iview.demand,iview.type,iview.pid, iview.wastage \n

How to query for Null or Missing Fields in Mysql X DevAPI?

How to query for null or missing field in Mysql X DevAPI?
I tried .find("age IS NULL") and .find("age = null") but both not work.
> db.createCollection('users')
> db.getCollection('users').add({ name: "foo", age: 30 })
> db.getCollection('users').add({ name: "bar", age: null })
> db.getCollection('users').find("age IS NULL")
Empty set (0.0003 sec)
> db.getCollection('users').find("age = null")
Empty set (0.0004 sec)
I'm able to reproduce that with the MySQL Shell (which I guess is what you are using) and with Connector/Node.js. I know for a fact that Connector/Node.js sends the correct datatype to the server (with the Shell you can check that with the --trace-proto option).
Mysqlx.Crud.Find {
collection {
name: "<some_schema>"
schema: "users"
}
data_model: DOCUMENT
criteria {
type: OPERATOR
operator {
name: "is"
param {
type: IDENT
identifier {
document_path {
type: MEMBER
value: "age"
}
}
}
param {
type: LITERAL
literal {
type: V_NULL
}
}
}
}
}
Which means it's some issue in the server.
In this case, looks like that X DevAPI expression is not resulting in the proper SQL query. If you look in the general log, you should see something like
SELECT doc FROM `<some_schema>`.`users` WHERE (JSON_EXTRACT(doc,'$.age') IS NULL)
The problem is that JSON_EXTRACT is returning null (JSON type) and not NULL (SQL "type"), and it is a limitation of the X Plugin.
One way for this to be fixed by the plugin is to replace JSON_EXTRACT() with JSON_VALUE(), which will return the proper NULL value in that case, but I don't know the implications of that.
As a workaround, you can always use
session.sql("select doc from `<some_schema>`.`users` where json_value(doc, '$.age') is null").execute()
In the meantime, I encourage you to report a bug at https://bugs.mysql.com/ using either the MySQL Server: Document Store: X Plugin or the MySQL Server: Document Store: MySQL Shell categories.
Disclaimer: I'm the lead developer of the MySQL X DevAPI Connector for Node.js

How to get id from text

I have value text:
{
"4384": {
"idRoomSv": 4384,
"NumRoom": 2,
"RoomId": 269
}
}
I want to get RoomId. It is return :269.
Can you help me? Thank very much!
If you have a recent version of MariaDB or MySQL, you can use the JSON_EXTRACT function.
Edit: try on your sql client the code below
SET #json = '{
"4384": {
"idRoomSv": 4384,
"NumRoom": 2,
"RoomId": 269
}
}';
SELECT JSON_EXTRACT(#json, '$.*.RoomId');
And the result is :
JSON_EXTRACT(#json, '$.*.RoomId')
1 [269]
The JSON_EXTRACT function accepts a JSON document for the firtst parameter. The second parameter is a JSONPath expression :
$ : is the root element
* : wildcard = all elements regardless their names
RoomId : value of that field

N1QL convert timezone into date,Couchbase

I want to convert 2015-10-29T16:15:11.000Z in to 2015-10-29 Date in N1QL query.
How can I do it ?
There are a couple of options here. The simplest way works if you can tolerate an ISO 8601 formatted time and uses the DATE_TRUNC_STR(expression, part) Date function:
SELECT DATE_TRUNC_STR(date_time, "day") AS new_date FROM bucket
The result for such a query (using 2015-10-29T16:15:11.000Z as date_time) is:
{
"results": [
{
"new_date": "2015-10-29T00:00:00Z"
}
]
}
Alternatively, you can parse out the individual parts using the DATE_PART_STR(expression, part) function with the TOSTRING(expression) function with String concatenation (||):
SELECT TOSTRING(DATE_PART_STR(date_time, "year")) || "-" || TOSTRING(DATE_PART_STR(date_time, "month")) || "-" || TOSTRING(DATE_PART_STR(date_time, "day")) AS new_date FROM bucket
The result:
{
"results": [
{
"new_date": "2015-10-29"
}
]
}
Reference:
https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/datefun.html

MongoDB equivalent to NOW() + INTERVAL

I'm currently working on converting our PHP backend from MySQL to MongoDB.
We are often using something like this in MySQL:
UPDATE table_1 SET completion_time = NOW() + INTERVAL 90 MINUTE WHERE id = 1;
How would I do this in MongoDB? Do I need to use 2 queries? First query to set completion_time with $currentDate and the 2nd query to increment it? I've read that $inc doesn't work on Dates in MongoDB, tho ...
You could try creating a date variable that holds the current date + 90 minutes later which you can then use to set the completion_time field with in your update:
var ninetyMinutesLater = new Date();
ninetyMinutesLater.setMinutes(ninetyMinutesLater.getMinutes() + 90);
db.table_1.update(
{ "_id": 1 },
{
"$set": {
"completion_time": ninetyMinutesLater
}
}
);
I see you have have tagged this with mongodb-php so I assume you are using PHP. MongoDB does have a $currentDate operator but currently there is no way to actually set an offset to that date.
For example I tried #diwakar's answer on 3.0:
> db.table_1.update({"id":1 },{completion_time: { $add: [ "$currentDate", 90 ] }})
2015-03-23T11:22:05.497+0000 E QUERY Error: field names cannot start with $ [$add]
at Error (<anonymous>)
at DBCollection._validateForStorage (src/mongo/shell/collection.js:160:19)
at DBCollection._validateForStorage (src/mongo/shell/collection.js:164:18)
at DBCollection._validateUpdateDoc (src/mongo/shell/collection.js:387:14)
at Object.findOperations.updateOne (src/mongo/shell/bulk_api.js:675:20)
at DBCollection.update (src/mongo/shell/collection.js:454:22)
at (shell):1:12 at src/mongo/shell/collection.js:160
So currently this needs to be done client side like so:
$mongo->collection->insert(['completion_time' => new MongoDate(time() + (60*90))])
It seems that is the only way.
Starting with MongoDB v5.0+, you can use $dateAdd with $$NOW.
db.collection.update({
_id: 1
},
[
{
$set: {
completion_time: {
"$dateAdd": {
"startDate": "$$NOW",
"unit": "minute",
"amount": 90
}
}
}
}
])
Here is the Mongo Playground for your reference.
use
db.collection.UpdateOne({ _id:id},{
$set:{
"completion_time": $add:[ $currentDate , (60*90)]
}
})
Please try this definitely work,
db.table_1.update({"id":1 },{completion_time: { $add: [ "$date", 90 ] }})