I have a mongoDB collection of documents. I field in the document is a Regex.
I want to take an input string from the user &
I want the Regex from the MongoDB to match the given string and echo out that resulting document.
eg:
"_id": {
"$oid": "5eeeef2a132cb8992da8"
},
"type": "integer",
"regex": "\d+",
"example": "12345"
}
If someone now inputs a string like 776847, it should go back to the collection compare the regex with the string and echo out integer, etc...
Any best methods to do this?
Thanks in advance.
If if understood you you mean that if the user input is : "1234"
and your mongo db is like :
"_id": {
"$oid": "5eeeef2a132cb8992da8"
},
"type": "integer",
"regex": "\d+",
"example": "12345"
}
The result will be integer.
you can extract all the mongo data and loop it
or
you can use mongo find method with mongodb cursor
example:
its in python but you will get the general idea
def func():
search = collection.find({'example':<user input>})
if search:
result = re.search(search['regex'],<user input>)
if result != []:
return = search['type']
else:
return "No match"
Hope it will help you
B.R
Shlomy
Related
I am trying to use JSON Schema to validate inputs to an API. The input follows a very simple schema:
{
"$schema": "https://json-schema.org/draft-07/schema",
"type": "object",
"properties": {
"some_key": {
"anyOf": [
{ "type": "integer" },
{ "type": "string", "pattern": "^(0|[1-9][0-9]*)$" }
]
}
},
"required": ["some_key"],
"additionalProperties": false
}
to match documents like:
{ "some_key": 1 }
{ "some_key": "12" }
{ "some_key": 22 }
{ "some_key": 42 }
{ "some_key": "42" }
Is there a way to specify in the schema that the strings should be converted to numbers? (and fail if it is not possible)
If it is impossible to convert the strings to numbers could I convert the numbers to strings?
The actual use case will have many more properties only some of which should be converted.
I know that JSON Schema includes some features that alter the input data (like default values), but I have not found reference to such a functionality.
If it matters this will be used in a PHP program likely with opis/json-schema library.
I know that JSON Schema includes some features that alter the input data (like default values), but I have not found reference to such a functionality.
This isn't right. JSON Schema only validates what's there; it doesn't alter anything. This is probably why you haven't found a reference to this functionality.
#Byted is correct in their comment. The conversion you want needs to be done by the host application.
{
"anyOf": [
{ "type": "integer" },
{ "type": "string", "pattern": "^(0|[1-9][0-9]*)$" }
]
}
Note that this will work to validate that you get an integer or a string that represents an integer, but you're not going to be able to use other numeric validations (like minimum) on the strings.
It seems you have provided the answer yourself. In the documentation of opis/json-schema there is a link to Support for casting.
That will probably do what you need.
Incoming JSON:
[
{
"name": "Name1"
},
{
"name": "Name2"
},
{
"name": "Name3",
"surname": "Surname3"
}
]
If we use JSONPath like $[:].name, we will receive:
[
0: "Name1",
1: "Name2",
2: "Name3"
]
But if will use the same to get surname ($[:].surname), we will receive:
[
0: "Surname3"
]
Is this possible to get surname values with empty string (or nulls) to keep right indexes? E.g.
[
0: "",
1: "",
2: "Surname3"
]
P.S.: at the moment I'm using this library.
JSON Path doesn't support returning placeholder values like that. It's a query language for JSON documents, much like SQL is for relational databases. Could you imagine if a SQL query returned placeholder values for every record in a database that didn't match your query?
I expect that the reason you want this is to determine where in the original document the value appears. To that end, JSON Path implementations should support returning the paths to the values instead of the values themselves:
[
"$[2]['surname']"
]
But I can't see in the README of that library where such a feature is supported (though it still might).
I am having a very difficult time figuring out how to retrieve the Google Drive folder associated with a particular Course (ID).
My understanding is that this value is available as the teacherFolder value:
See Here:
https://developers.google.com/classroom/reference/rest/v1/courses
"id": string,
"name": string,
"section": string,
"descriptionHeading": string,
"description": string,
"room": string,
"ownerId": string,
"creationTime": string,
"updateTime": string,
"enrollmentCode": string,
"courseState": enum(CourseState),
"alternateLink": string,
"teacherGroupEmail": string,
"courseGroupEmail": string,
"teacherFolder": {
object(DriveFolder)
},
But this is returned as an object that contains:
{
"id": string,
"title": string,
"alternateLink": string,
}
I am trying to get the Google Drive folder associated with the course in Google Classroom (by Course ID) so that I can upload a resource to the appropriate folder.
Please point me in the right direction.
(I am hoping to accomplish this via php.)
Based from the documentation:
Resource: Course
{
"id": string,
"name": string,
"section": string,
"descriptionHeading": string,
"description": string,
"room": string,
"ownerId": string,
"creationTime": string,
"updateTime": string,
"enrollmentCode": string,
"courseState": enum(CourseState),
"alternateLink": string,
"teacherGroupEmail": string,
"courseGroupEmail": string,
"teacherFolder": {
object(DriveFolder)
},
"courseMaterialSets": [
{
object(CourseMaterialSet)
}
],
"guardiansEnabled": boolean,
}
You can check out the CourseMaterialSet property which hold all related marterials to the course.
CourseMaterialSet
A set of materials that appears on the "About" page of the course. These materials might include a syllabus, schedule, or other background information relating to the course as a whole.
{
"title": string,
"materials": [
{
object(CourseMaterial)
}
],
}
CourseMaterial
A material attached to a course as part of a material set.
{
// Union field material can be only one of the following:
"driveFile": {
object(DriveFile)
},
"youTubeVideo": {
object(YouTubeVideo)
},
"link": {
object(Link)
},
"form": {
object(Form)
},
// End of list of possible types for union field material.
}
Hope this helps.
I have the following structure in mongo
{
"_id": ObjectId("5188deba4c2c989909000000"),
"_type": {
"0": "Model_Discs"
}
},
"title": "really cool cd",
"referencedBy": {
"0": {
"$ref": "discs",
"$id": ObjectId("4e171cade3a9f23359e98552")
},
"1": {
"$ref": "discs",
"$id": ObjectId("5045c3222b0a82ec46000000")
}
}
}
This is actually an indexed array that holds references to other documents. I to find all the documents that have a reference to ObjectId("5045c3222b0a82ec46000000").
At first I wrote "referencedBy.1.$id": ObjectId("5045c3222b0a82ec46000000") which doesn't work (as expected) because the ObjectId can be found under different index in other documents
i.e.
referencedBy.1.$id
referencedBy.5.$id
referencedBy.3.$id
So I need to find all documents that reference my ObjectId who are anywhere under the referencedBy DocumentSet. Something like
"referencedBy.*.$id": ObjectId("5045c3222b0a82ec46000000")
I don't quite see why you have such a complicated structure. Particularily the "0" and "1" keys are problematic, especially dealing with PHP as it doesn't really like arrays with numerical string keys. The $ref/$id fields come from MongoDBRef, which should avoid as they don't provide you with any functionality.
You should just have:
{
"_id": ObjectId("5188deba4c2c989909000000"),
"_type": "Model_Discs",
"title": "really cool cd",
"referencedBy": [
ObjectId("4e171cade3a9f23359e98552"),
ObjectId("5045c3222b0a82ec46000000")
]
}
Then you can simply query with:
db.collection.find( { referencedBy: new ObjectId("5045c3222b0a82ec46000000") } );
I have a key in my document whose structure is as follow:
"tag": [
{
"schemeName": "http:\/\/somesite.com\/categoryscheme2",
"name": "Test Tag2",
"value": 1,
"slug": "test_tag2"
},
{
"schemaName": "http:\/\/somesite.com\/categoryscheme3",
"name": "Test Tag3",
"value": 1,
"slug": "test_tag3"
}
]
Now, I get inputs as tag=test_tag2ANDtest_tag3. How can I write a query for this?
I tried to iterate through the loop but I didnt got any results.
Correct me if I am wrong but you don't need an $and or $elemMatch, instead:
$mongodb->collection->find(array('tags.slug'=>array(
'$in' => array('test_tag2','test_tag3'))))
Should work, however, if your English suggests what a second read does, then you can also use $all in place of $in. This will ensure that all root documents must have those slugs in them.
use $elemMatch operator to match elements inside the array.