How to add invalid values enum in json schema - json

If we want to add valid values for particular field in json schema, we will add like this,
"Car_Color" :{
"type":"string",
"enum" :["blue", "red"]
}
I want user to enter any value for this field but Orange.
I am expecting something like this,
"Car_Color" :{
"type":"string",
"Not-contain" : "orange"
}
Since there are many possibilities we can't do it using "enum". Is there anything opposite to enum. So that value can't be entered.
I want user to enter any value for this field but Orange.
I am expecting something like this,
"Car_Color" :{
"type":"string",
"Not-contain" : "orange"
}
I tried with "not" but it is not working. If any solution for this one please provide.

You are quite close. You can use the not keyword to express what you want like this:
"Car_Color" : {
"type":"string",
"not": { "enum" :["orange", "..."] } }
}

Related

JPath for filtering properties not working

I'm new at JSONPath so I though maybe you could help me.
I've got the following JSON:
{
"records":[
{
"id":"recGyk2cLCzWoCFLj",
"fields":{
"Etapa":0,
"Enviado por":"EFG",
"Telemóvel":"919919919",
"ID":1,
"Nome":"Manuel"
},
"createdTime":"2022-01-31T20:40:45.000Z"
},
{
"id":"rect2YP8RF2Jm88Cw",
"fields":{
"Etapa":0,
"Enviado por":"CDE",
"Telemóvel":"919919919",
"ID":2,
"Nome":"Maria"
},
"createdTime":"2022-01-31T20:40:45.000Z"
},
{
"id":"rectoQCinQrYvMJFb",
"fields":{
"Etapa":0,
"Enviado por":"ABC",
"Telemóvel":"919919919",
"ID":3,
"Nome":"Joaquim"
},
"createdTime":"2022-01-31T20:40:45.000Z"
}
]
}
I'm trying to get the ID (the capital letter one) from the fields where 'Etapa==0'.
I've tried lots of combinations and $.records[?(#.fields.Etapa==0)].fields.ID seem like it could work but it doesn't.
When I try $.records[?(#.id=="rectoQCinQrYvMJFb")].fields.ID it works, so what could I be doing wrong?
Thanks!
Edit: The expression was right, it needed quotes on the zero for the $.records[?(#.fields.Etapa=="0")].fields.ID expression as the field type was a string.

Incompatible value for Quickbase Date Field

I have a feature where I update the values on Quickbase for our system.
I am able to update most fields, checkboxes, text inputs and numerical data..
using this kind of query
{
"to":"appid",
"data": [
{
"3": { "value": 1 },
"308": { "value": "2021-5-17" },
"104": { "value": true }
}
]
}
but when I try updating a value on a date field.. I get a '207 Multi-Status' response from it.
any idea how to set date values?
I tried different string formats. Quickbase formulas/functions like 'today()'
Thanks!
The format of your date is not quite correct. This API is very strict about the format YYYY-MM-DD so you should use "308": { "value": "2021-05-17" }. You can use some other keywords such as today for the value as described in the field type documentation. Also, if you are actually using the application Id for appId that will also cause problems since a table ID is expected there instead.
There could be other errors and the 207 Multi-Status code alone doesn't give much of a hint about what went wrong. If you can, look at the response body where you should see an error description returned from Quickbase that would look something like this:
{
"data": [],
"metadata": {
"createdRecordIds": [],
"lineErrors": {
"1": [
"Incompatible value for field with ID \"308\"."
]
},
"totalNumberOfRecordsProcessed": 1,
"unchangedRecordIds": [],
"updatedRecordIds": []
}
}

Can JSON schema validate exactly one property contains a certain property?

I was trying to come up with a schema to validate JSON objects like the following:
{
"id":"some_id",
"properties":{
"A":{
"name":"a",
"isindex":true
},
"B":{
"name":"b"
},
"C":{
"name":"c"
}
}
}
The deal is:
properties A, B, C are not known beforehand and can be any strings.
One and only one of the properties (A, B, C ...) has in its value a "isindex":true key-value pair to indicate the property will be used as a index. That is to say the following is invalid.
.
{
"id":"some_id",
"properties":{
"A":{
"type":"string",
"isindex":true
},
"B":{
"type":"string"
},
"C":{
"type":"array",
"isindex":true
}
}
}
Actually, I am not sure if the JSON schema is the right tool for for this.
Any or all help is appreciated!
JSON Schema is the right tool for this kind of thing, but you have stumbled on a specific case that it doesn't handle. You can assert that at least one matches a particular schema, but you can't assert that one and only one matches.
The best thing you can do is change your data structure to something like this ...
{
"id":"some_id",
"properties":{
"A":{
"name":"a"
},
"B":{
"name":"b"
},
"C":{
"name":"c"
}
},
"index": "A"
}

Json array in mongoDB

I want to get objects according to an ID they have in an array in a json file in mongodb.
I tried a lot of ways to get them with no success:
db.collection.find({"Id":"2"})
db.collection.find({"Messages.Id":"2"})
db.collection.find({"Messages":{$elemMatch:{"Id":"2"}}})
db.collection.find({"Messages.Id":{$elemMatch:{"Id":"2"}}})
{
"Messages" : [
{
"text":"aaa",
"Id" : [ "1", "2" ]
},
{
"texts" : "bbb",
"Id" : [ "1", "3" ]
}
]
}
Even though that's how it's supposed to be done according to the mongodb documentation.
So I thought something was wrong with my json design (I tried changing it but that didn't help either).
Can anyone suggest to me a good design or query to get the objects with a certain id will work?
UPDATE:
I want for example that if in the query i request the id 2
only the first message and all of it will be displayed (I don't mind if the Id field wont be displayed)
{
"text":"aaa",
"Id":["1","2"]
}
To find single elements that match you will need to utilize the positional operator ($).
db.collection.find({"Messages.Id": "2"}, {"Messages.$": 1, _id: 0})
For finding multiple matches, you would use the aggregation pipeline:
db.collection.aggregate([
{ $unwind: "$Messages" },
{ $match: {"Messages.Id": "1"}},
{ $group: { _id: null, messages: { $push: "$Messages"}}}
])

MongoDB AND Comparison Fails

I have a Collection named StudentCollection with two documents given below,
> db.studentCollection.find().pretty()
{
"_id" : ObjectId("52d7c0c744b4dd77efe93df7"),
"regno" : 101,
"name" : "Ajeesh",
"gender" : "Male",
"docs" : [
"voterid",
"passport",
"drivinglic"
]
}
{
"_id" : ObjectId("52d7c6a144b4dd77efe93df8"),
"regno" : 102,
"name" : "Sathish",
"gender" : "Male",
"dob" : ISODate("2013-12-09T21:05:00Z")
}
Why does the below query returns a document when it doesn't fulfil the criteria which I gave in find command. I know it's a bad & stupid query for AND comparison. I tried this with MySQL and it doesn't return anything as expected but why does NOSQL makes problem. I hope it's considering the last field for comparison.
> db.studentCollection.find({regno:101,regno:102}).pretty()
{
"_id" : ObjectId("52d7c6a144b4dd77efe93df8"),
"regno" : 102,
"name" : "Sathish",
"gender" : "Male",
"dob" : ISODate("2013-12-09T21:05:00Z")
}
Can anyone brief why does Mongodb works this way?
MongoDB leverages JSON/BSON and names should be unique (http://www.ietf.org/rfc/rfc4627.txt # 2.2.) Found this in another post How to generate a JSON object dynamically with duplicate keys? . I am guessing the value for 'regno' gets overridden to '102' in your case.
If what you want is an OR query, try the following:
db.studentCollection.find ( { $or : [ { "regno" : "101" }, {"regno":"102"} ] } );
Or even better, use $in:
db.studentCollection.find ( { "regno" : { $in: ["101", "102"] } } );
Hope this helps!
Edit : Typo!
MongoDB converts your query to a Javascript document. Since you have not mentioned anything for $and condition in your document, your query clause is getting overwritten by the last value which is "regno":"102". Hence you get last document as result.
If you want to use an $and, you may use any of the following:
db.studentCollection.find({$and:[{regno:"102"}, {regno:"101"}]});
db.studentCollection.find({regno:{$gte:"101", $lte:"102"}});