How to query for a field name that can be in different levels in mongodb - mongodb-query

Is it possible to query MongoDB for values of a specific field-name disregarding where it's located in the document structure?
For example If I have this document: https://mongoplayground.net/p/ANSezUHhueD
{
device_name: "host1",
ip: 192.168.1.10,
connected_hosts: [
{
name: "host2",
ip: 192.168.1.11
},
{
name: "host2",
ip: 192.168.1.12
} ​
​]
}
Can I look for all the fields named "ip" no matter where they are defined, so I get a result that lists all 3 IPs [192.168.1.10, 192.168.1.11, 192.168.1.12]?
In my use case the nested document {name, ip} can appear in additional different other nested documents.

Related

Open API schema conditional response field based on the presence of a query parameter

I am working on providing a GET REST API where I would like to conditionally include the total_documents field (its an integer count of the total number of records present in the DB table).
The API signature and response payload will be something like:
GET /endpoint/?total_documents&.....
Response Payload:
{
documents: [....],
total_documents: 100
}
Now I would like the total_documents field to be appeared in the response payload if and only if the total_documents query parameter exists in the URL.
This is what I tried, based on my schema:
fastify.addSchema({
$id: 'persistence-query-params',
title: "PersistenceQueryParams",
type: 'object',
description: 'Persistence Service GET API URL query specification. Applicable for GET API only.',
properties: {
'total_documents': {
description: 'Total number of documents present in the collection, after applying filters, if any. This query paramater does not take any value, just pass it as the name (e.g. &total_documents).',
nullable: true,
},
},
}
querystring: {
description: 'Persistence Service GET API URL query specification. Applicable for GET API only.',
$ref: 'persistence-query-params#',
},
response: {
200: {
properties: {
'documents': {
description: 'All the retrieved document(s) from the specified collection for the specified service database and account.',
type: 'array',
items: {
$ref: 'persistence-response-doc#',
}
},
'total_documents': {
description: "If total_documents query paremeter is specified, gives the total number of documents present in the collection, after applying query paramaters, if any. If total_documents is not specified, this field will not be available in the response payload.",
type: 'number',
default: -1,
},
},
dependencies: {
'total_documents': { required: ['querystring/properties/total_documents'] },
},
},
'4xx': {
$ref: 'error-response#',
description: 'Error response.'
}
}
What is the way out here?
Thanks,
Pradip
JSON Schema has no notion of a request or response or HTTP.
What you have here is an OpenAPI specification document.
The OpenAPI specification defines a way to access dynamic values, but only within Link Objects or Callback Objects, which includes the query params.
Runtime expressions allow defining values based on information that
will only be available within the HTTP message in an actual API call.
This mechanism is used by Link Objects and Callback Objects.
https://spec.openapis.org/oas/v3.1.0#runtime-expressions
JSON Schem has no way to reference instance data, let alone data relating to contexts it is unaware of.

How to use Atlassian Document Format in create issue rest api

I am trying to create an issue via Jira API -
{
// other fields is here
description: {
type: "doc",
version: 1,
content: [
{
type: "text",
text: summary
}
}
}
but I get an error - "Operation value must be a string".
so how can I create an issue correctly?
Most likely you're using API version 2 - which accepts text for this field.
However, you're providing value as json (Atlassian Document Format) which is for API version3

Creating a titled Google Sheets results in a "Proto field" error when using the NodeJs client library

I am trying to create a Google Spreadsheet using a NodeJs backend and the Google Sheets v4 API.
I was following the spreadsheets.create tutorial in documentation. However, when I create the file using some specified properties, I always get the following error:
Error: Invalid JSON payload received. Unknown name "title" at 'spreadsheet.properties': Proto field is not repeating, cannot start list.
In the tutorial nothing, is mentioned about a "Proto" field. Is this a bug or am I missing something?
Creating the file does work, if I don't specify properties. However the properties are used to set a name for the file and the sheets, so I do need a way to set this metadata.
Here is the request I am sending with the properties included:
const request = {
auth,
resource: {
properties: {
title: name,
},
sheets: [
{
properties: {
title: 'General',
},
},
],
},
};

Symfony 4 one entity, two entity Manager

Hello everyone, I'm trying to have 2 entityManagers for one entity in Symfony4 but I have some trouble to do this.
When I persist an entity it works,(For example if have two entity Mananagers : Customer and Default ,when I use Customer or Default to persist) but when I want to use Repository, The first entity Managare in doctrine.yaml is always used.
I have to do this because I have 2 databases. One in internet and one inside my intranet that i have created and I search to do is that when the user click one button for example. It update the database on internet.
config/packages/doctrine.yaml
doctrine:
dbal:
default_connection: default
connections:
default:
driver: pdo_mysql
host: **************
port: 3306
dbname: intranetDb
user: **********
password: *****
charset: UTF8
customer:
driver: pdo_mysql
host: internetDb
port: 3306
dbname: *********
user: *********
password: *********
charset: UTF8
orm:
default_entity_manager: default
entity_managers:
default:
connection: default
auto_mapping: false
mappings:
Main:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: Main
customer:
connection: customer
auto_mapping: false
mappings:
Customer:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: Main
MyController.php
..
$drug = $this->getDoctrine()->getRepository(Drug::class,'customer')->findAll() ;
..
This code always give me the data inside of default and if I put customer first inside of orm the customer is always given.
Some help will be welcome because I have this problem in few days and I have no Idea to solve this(It's probably because of symfony version that I didn't found solution inside forum).
Thank you.(And sorry for my bad English)
You can get the repository from an entity manager, instead of getting it from the ManagerRegistry returned by getDoctrine().
Example:
[...]
$this->getDoctrine()->getManager('manager_name')->getRepository('class_name');
[...]

RAML : How to require parameter A OR parameter B

I'm writing some REST documentation with RAML but I'm stuck.
My problem:
- I have a GET request used for search that can take a parameter "id" or (exclusive or) "reference". Having only one of them is required.
I know how to say "this param is required" but I don't know how to say "having one of these params is required". Is it even possible?
The following example written in RAML 1.0 defines two object types in Url and File then creates another object Item which requires Url OR File in ext. If you change the included examples (which currently validate), you'll see that they fail if the property does not conform to one or the other definition. Hope that helps! LMK if you have any other questions and I'll do my best.
[EDIT: hmm I think I am seeing your problem now, the final example I've just added, named should_fail, (which has one of each type together in the example) still validates and you want a way to make it fail validation.]
[UPDATE: OK I figured a mildly hacky way to do this. Use maxProperties: 1 in the object which should have properties appear alone, see updated code below which fails the final example during validation.]
#%RAML 1.0
types:
Url:
properties:
url:
type: string
example: http://www.cats.com/kittens.jpg
description: |
The url to ingest.
File:
properties:
filename:
type: string
example: kittens.jpg
description: |
Name of the file that will be uploaded.
Item:
description: |
An example of a allowing multiple types yet requiring
one AND ONLY one of two possible types using RAML 1.0
properties:
ext:
maxProperties: 1
type: File | Url
examples:
file_example:
content:
ext:
filename: video.mp4
url_example:
content:
ext:
url: http://heres.a.url.com/asset.jpg
should_fail:
content:
ext:
url: http://heres.a.url.com/asset.jpg
filename: video.mp4
I had the same problem. User can provide either a textual input OR a file input, but not both.
Both have different fields and I detect the request type from the field names. i.e if the request has [files and parameters], it is a FileInput. If the request has [texts and parameters], it is a TextInput. It is not allowed to provide both text and file within the same request.
I used the union property. See CatAndDog example in
Raml 200 documentation for a small example.
You can define your types as follows.
types:
FileInput:
properties:
parameters:
type: Parameters
description: (...)
files:
type: ArchiveCollection | FileCollection
description: (...)
TextInput:
properties:
parameters:
type: Parameters
description: (...)
texts:
type: TextCollection
description: (...)
Then in my POST request body:
/your_route:
post:
body:
multipart/form-data:
type: TextInput | FileInput
The fields in the body are defined with either TextInput or FileInput type.
In RAML 0.8 you can not describe queryParameters with only one parameter.
In RAML 1.0 you can do this. You should use oneOf in jsonschema for describing Type. Your queryParameters should use this type. Example:
api.raml
#%RAML 1.0
title: AUTH microservice
mediaType: application/json
protocols: [HTTPS]
types:
- example: !include schemas/example.json
/example:
get:
queryParameters:
type: example
schemas/example.json
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"id": "file://schemas/credentials.json",
"oneOf": [
{
"properties": {"key1": {"type": "string"}},
"additionalProperties": false
},
{
"properties": {"key2": {"type": "string"}},
"additionalProperties": false
}
]
}
Also you can use uriParameters. Maybe it will help in your case.
#%RAML 0.8
title: API Using media type in the URL
version: v1
/users{mediaTypeExtension}:
uriParameters:
mediaTypeExtension:
enum: [ .json, .xml ]
description: Use .json to specify application/json or .xml to specify text/xml