How do you filter by partial match in array of strings in Prisma? - prisma

Say I have the following Prisma schema:
model Article {
id String #db.ObjectID
tags String[]
}
There are articles like this in the database:
id: 1, tags: [ 'tag1', 'tag2', 'tag3' ]
id: 2, tags: [ 'taga', 'tag1' ]
id: 3, tags: [ 'water', 'tag3' ]
How could I make an article.findMany filter where a string could be partially matched against all the tags?
So for example, if my search string is ag1 it will return ids 1 and 2, or if the search string is at, it would return id 3.

Related

Find matching field in an array of objects in an object with go mongo-driver

My mongo database has a structure like this. Given a parent _id, for example, 123, how can I make a query to check if item abc exists in its parent 123?
[
- _id: 123
name: "item 1"
items: [
{
_id: abc,
age: 12,
},
{
_id: efg,
age: 12,
}
]
,
- id: 456
name: "item 2"
items: [
...
]
]
I currently have this and I've tried $elemMatch but it doesn't seem to work.
db.Collection("album").FindOne(context.Background(), bson.M{"_id": parentID})
It is not clear if items has separate documents, or if it is a nested array in the document. Either case:
If items is a nested array in the document, then:
bson.M{"_id":parentID,"items._id":"abc"}
will find documents whose _id is parentID and that contains _id:"abc" in one of the elements of items.

MongoDB: Get documents depending on a search term for array field and string field

I need to perform a search on my mongoDB collection based on two fields: title, which is a string, and tags, which is an array.
Assume this is a sample data for the collection:
{
title: 'A tornado article',
tags: [
'nature',
'storm'
]
},
{
title: 'Just another article',
tags: [
'hurricane',
'type I'
]
},
{
title: 'Different article',
tags: [
'tornado',
'type II'
]
}
and my search string is const term = 'tornado type I', then I should get all documents as
the first one has tornado in the title
the second one has an tag type I
the third one has an tag tornado
I tried to start with this to perform the search:
Collection.find(
{
$or: [
{ 'meta.title': new RegExp(term, 'i') },
{ 'meta.tags': term }
]
}
)
So with this I would get the first document if the search term would be just tornado. With my sample term string I don't get any result.
If you wish to return an object that contains any of the tags you need to the the $in operator, it selects the documents where the value of a field equals any value in the specified array.
However to do so you need to pass it an array of your terms, to do so I've splitted your terms on
" " and passed that variable along to the $in like so
var term = 'tornado type I nature';
var splitted = term.split(' ');
Collection.find({
$or: [{
'title': new RegExp(term, 'i')
}, {
"tags": {
$in: splitted
}
}
]
})
Or if you also use to wish the regex to search through the tags you could join the array with | to build a regex statement after which you can pass it along like this.
var term = 'tornado type I nature';
var splitted = term.split(' ');
var splittedRegex = new RegExp(splitted.join('|'), 'i');
Colleciton.find({
$or: [{
'title': new RegExp(term, 'i')
}, {
"tags": {
$in: [splittedRegex]
}
}
]
})

Building mongo query

I have a model like this:
[{item: {
_id: 123,
field1: someValue,
price: {
[_id: 456,
field1: anotherValue,
type: []],
[_id: 789,
field1: anotherValue,
type: ['super']]
}
}]
I need to find an item by 3 parameters: item _id, price _id, and check if price type array is empty. And check it in one price field.
Model.findOneAndUpdate({_id: 123, "price._id": 456, "price.type": {size:0})
This query always returns item, cause search in different prices.
Model.findOneAndUpdate({_id: 123, price: {id: 456, type: {size:0})
This query returns error (cast array value or something like this).
tried to build query with $in, $and, but still getting an error
Use $elemMatch:
The $elemMatch operator matches documents that contain an array field
with at least one element that matches all the specified query
criteria.
db.inventory.find({
price: {
"$elemMatch": {
_id: 456,
type: {
$size: 0
}
}
}
})

MongoDb Text Search On Different fields

My collection looks this:
{ _id: 1, author: "Jack London", bookName: "The Sea-Wolf" }
{ _id: 2, author: "Alex Kershaw", bookName: "Jack London" }
I want to be able to search by author or by bookName only, but MongoDB allows you to create only 1 text index. Thanks for help.
For Example:
I wanna search for author only :
db.collection.find({$text:{$search:"Jack London"}})//search in 'author' field
Or by bookName only :
db.collection.find({$text:{$search:"Jack London"}})//search in 'bookName' field
If I have compound index {author: "text", bookName: "text"} it will return me both entries, which is not what I want.

How can I search an array of subdocument fields that match a given array

Say I have a collection of recipes that match this format:
{
title: "a recipe",
ingredients: [
{
description: "sugar",
amount: "1 cup"
},
{
description: "flour",
amount: "2 cups"
}
]
}
If given an array such as ["sugar", "butter"], what is the best way to return all recipes that have an ingredient whose description is in the given array?
MongoDB unwinds the arrays while querying. So this query ought to do it.
{'ingredients.description':{$in:["sugar", "butter"]}}