Atlas autocomplete in array - mongodb

I am trying to create a search index on atlas for a field in an object in an array.
However, the index I created doesn't work.
Example of the data:
{
"_id": "zyicGj7d2R3ezKB6s",
"arrayData": [{
_id: 'roGxCLhJPhQ7NLNed',
name: 'test'
},
{
_id: 'ziPszDHvSYXroLuFm',
name: 'test2'
}
],
}
The index I have at the moment is
{
"mappings": {
"dynamic": false,
"fields": {
"arrayData.name": {
"tokenization": "nGram",
"type": "autocomplete"
}
}
}
}
For the search I need it to be type autocomplete and I want to search on the name in the object's

Related

MongoDB -Query documents where nested array fields is equal to some value

I have a JSON object:
{
"ownershipSheetB": {
"lrUnitShares": [{
"description": "blabla1",
"lrOwners": [{
"lrOwnerId": 35780527,
"name": "somename1",
"address": "someadress1",
"taxNumber": "12345678910"
}
],
"lrUnitShareId": 29181970,
"subSharesAndEntries": [],
"orderNumber": "1"
}, {
"description": "blabla2",
"lrOwners": [{
"lrOwnerId": 35780528,
"name": "somename2",
"address": "someadress2",
"taxNumber": "12345678911"
}
],
"lrUnitShareId": 29181971,
"subSharesAndEntries": [],
"orderNumber": "2"
}
],
"lrEntries": []
}
}
I would like to query all documents that have taxNumber field equal to some string (say "12345678911" from the example above).
I have tried this query:
{"ownershipSheetB.lrUnitShares": { "lrOwners": {"taxNumber": "12345678910"}}}
but it returns no documents.
Solution 1: With dot notation
db.collection.find({
"ownershipSheetB.lrUnitShares.lrOwners.taxNumber": "12345678910"
})
Demo Solution 1 # Mongo Playground
Solution 2: With $elemMatch
db.collection.find({
"ownershipSheetB.lrUnitShares": {
$elemMatch: {
"lrOwners": {
$elemMatch: {
"taxNumber": "12345678910"
}
}
}
}
})
Demo Solution 2 # Mongo Playground

MongoDB - How to add a field to all object within an array

I have a document with a field called info, and info has a field inside it called data. data is an array of objects. I want to add a new boolean field, isActive: false, to each object in data, with updateMany.
This is how it looks now
{
info: {
data: [{
"name": "Max"
},
{
"name": "Brian"
},
...
]
}
}
This is what I want:
{
info: {
data: [{
"name": "Max",
"isActive": false
},
{
"name": "Brian",
"isActive": false
},
...
]
}
}
How do I do that?
Add the isActive field with all positional operator $[].
db.collection.update({},
{
$set: {
"info.data.$[].isActive": false
}
},
{
multi: true
})
Consider applying { multi: true } if you want to update multiple documents.

Mongoose with search autocomplete index

I want an autocomplete search on a name field in my collection on MongoDB Atlas. I've configured a search index in MongoDB atlas as following:
{
"mappings": {
"dynamic": false,
"fields": {
"name": {
"foldDiacritics": false,
"maxGrams": 8,
"type": "autocomplete"
}
}
}
}
and I'm trying to search via mongoose a substring in the entire collection like this:
collection.aggregate([
{
$search: {
autocomplete: {
path: 'name',
query: query,
},
},
},
{
$limit: 10,
},
{
$project: {
_id: 0,
name: 1,
},
},
]);
I always get 0 results, does anyone can help me understand what am I doing wrong?
Thanks,
Erez
The problem was that I've changed the default index name (which is 'default') and in that case, I needed to specify the new index name in the query or change the index to be named 'default'. Didn't found any mention of that in the MongoDB documentation though.

Mongodb Atlas Search with directive insensitive

I am using MongoDB Atlas Search to perform a search in Collection, for this I created a Atlas Search Index:
{
"mappings": {
"dynamic": false,
"fields": {
"caption": {
"type": "string"
}
}
}
}
Here is my aggregation:
[
{
"$search":{
"text":{
"path":"caption",
"query":"Ingocnitáá",
"fuzzy":{
}
},
"highlight":{
"path":"caption"
}
}
}
]
I have below document in my collection:
{caption:"Ct tyu test Ingocnitáá"}
Issue: When I searching Ingocnitaa agreegation returning 0 result.
Is there anything wrong with my Search Index? I want an directive insensitive Search with highlight.
There are two things missing:
Include index name (Recommend to not use a default index, create a new index)
Always pass fuzzy:{}
Here is a working query:
[
{
"$search":{
"index": 'messageText',
"text":{
"path":"caption",
"query":"Ingocnitaa",
"fuzzy":{
}
},
"highlight":{
"path":"caption"
}
}
}
]
Where messageText is search index name.
Search Index Formattion:
{
"mappings": {
"dynamic": false,
"fields": {
"caption": {
"type": "string"
}
}
}
}
Reference: CLick here

Update a subitem if it exists or insert it if not in mongodb

I'm new in here and I've just started to uses mongodb recently. I have a problem, I'm trying to update a collection's item in a given model if the item exists or update it if not.
I tried the following code but it's not working:
const res = await this.userModel.findByIdAndUpdate({
_id: user.id,
// 'devices.name': 'postman2'
}, {
$addToSet: { 'devices.[]': { name: 'postman2', generatedSecret: generatedSecret } }
}, { new: true, upsert: true }).exec();
My document looks like this:
{
"_id": {
"$oid": "5e9c6ffe9463db1594a74bec"
},
"email": "john.doe#mail.com",
"logins": [{
"provider": "email",
"secret": "sha1$e3d548b5$1$6f1b28e6b7cef47ca27b4e55ddbb6a5b8bc6b0ce"
}],
"devices": [{
"_id": {
"$oid": "5e9ddc9adb866666845bf86b"
},
"name": "postman",
"generatedSecret": "$2b$10$7fmneXGS1FjKyPXBa2Ea1erQfXF3ALjylIxOhetA9yxc3S95K4LVO"
}],
"applications": [{
"applicationId": "5e9c8b7c9463db1594a74bed"
}]
}
I want to update that devices item which matchs the query conditions. But if there isn't any matched result, then insert a new item with searched parameters.
Is possible to do this in one command?
Thanks!