How to search character by character in mongodb array text field? - mongodb

I have documents in mongodb are like :
[{
"_id" : 1,
"name" : "Himanshu",
"tags" : ["member", "active"]
},{
"_id" : 2,
"name" : "Teotia",
"tags" : ["employer", "withdrawal"]
},{
"_id" : 3,
"name" : "John",
"tags" : ["member", "deactive"]
},{
"_id" : 4,
"name" : "Haris",
"tags" : ["employer", "action"]
}]
What I want to search here is if we have array of filter like {"tags" : ["member", "act"]} it will reply back id's 1 and 2 because here member is full match and act partial match in two documents.
if I have filter like {"tags" : ["mem"] } then it should reply id's 1 and 3
One more case If I have filter like {"tags" : ["member", "active"]} then it should answer only 1.

You basically need two concepts here.
Convert each input string of the array into an Regular expression anchored to the "start" of the string:
Apply the list with the $all operator to ensure "all" matches:
var filter = { "tags": [ "mem", "active" ] };
// Map with regular expressions
filter.tags = { "$all": filter.tags.map(t => new RegExpr("^" + t)) };
// makes filter to { "tags": { "$all": [ /^mem/, /^active/ ] } }
// search for results
db.collection.find(filter);

Related

Mongo how to exclude nested _id in the nested array

I want to exclude nested default _id in every element of array in the founded result.
I have next request. In some fields _id property can be absent.
How can I exclude it in the filters ?
Is it possible?
db.getCollection('Test').find({"name":"t1"},{"_id":0})
I get next data from DB
{
"array" : [
{
"_id": ObjectId("5685ea32ba5298688d27cceb"),
"id" : 1,
"name" : "aaa"
},
{
"_id": ObjectId("5685ea32ba5298688d27cceb"),
"id" : 2,
"name" : "bbb"
},
{
"id" : 3,
"name" : "bbb"
},
]
}
I want to get next array without _id
{
"array" : [
{
"id" : 1,
"name" : "aaa"
},
{
"id" : 2,
"name" : "bbb"
},
]
}
When I try to use
db.getCollection('Test').find({"name":"t1"},{"_id":0, "array._id":0})
I get the error like:
"errmsg" : "Projection cannot have a mix of inclusion and exclusion.",
I think the reason that some objects of array don't contain the _id property
The _id is inside an array field, so you have to use array._id,
db.getCollection('Test').find(
{ "name": "t1" },
{ "array._id": 0 }
)
Playground,
Mongo Shell

Mongodb sorting issue

My mongodb collection:
[{
"_id" : ObjectId("5dd6598d55396f36052e347d"),
"isActive" : true,
"myarray" : [
{
"my_id" : "5d967d08821b4031a197b002",
"name" : "jack"
},
{
"my_id" : "5d967d2c821b4031a197b003",
"name" : "manison"
}
]
},
{
"_id" : ObjectId("5dd6598d55396f36052e347d"),
"isActive" : true,
"myarray" : [
{
"my_id" : "5d967d08821b4031a197b002",
"name" : "penelope"
},
{
"my_id" : "5d967d2c821b4031a197b003",
"name" : "cruz"
}
]
}]
Here i am trying to sort based on the name.
not expecting to sort inside the array but expecting outside.
Expecting result be like
[{
"_id" : ObjectId("5dd6598d55396f36052e347d"),
"isActive" : true,
"myarray" : [
{
"my_id" : "5d967d08821b4031a197b002",
"name" : "penelope"
},
{
"my_id" : "5d967d2c821b4031a197b003",
"name" : "cruz"
}
]
},{
"_id" : ObjectId("5dd6598d55396f36052e347d"),
"isActive" : true,
"myarray" : [
{
"my_id" : "5d967d08821b4031a197b002",
"name" : "jack"
},
{
"my_id" : "5d967d2c821b4031a197b003",
"name" : "manison"
}
]
}]
"name" : "cruz" coming first because alphabatically C comes fast than J AND M (which is in second json)
And Prenelop and cruz didn't switched just the main document json switched as per the name order
Query i am using
db.traffic.aggregate([
{$unwind: "$customFieldArray"},
{$sort: {"customFieldArray.field_value":1}},
{$group: {_id:"$_id", customFieldArray: {$push:"$customFieldArray"}}}
]);
But it is sorting inside like taking cruz to penelope and vice versa.
And main json staying stable.
Please have a look
You can do with simple find query with sort cursor
db.traffic.find({}).sort({ "myarray.name": -1 })
From the docs
With arrays, a less-than comparison or an ascending sort compares the
smallest element of arrays, and a greater-than comparison or a
descending sort compares the largest element of the arrays.

Mongo Query for 2 values in array list in a document field

I have a document in a collection that has a field called "myList", it has list items and I need to be able to query collection documents that have field "status" of "good" and "doneBy" with a value of "system" in the "myList" field:
[collection].myList
[
{
"location" : "3826487.pdf",
"status" : "good",
"time" : ISODate("2017-06-27T20:03:46.512Z"),
"reportIdx" : 0,
"doneBy" : "System"
},
{
"location" : "rt-0.pdf",
"status" : "bad",
"time" : ISODate("2017-06-28T16:24:16.559Z"),
"reportIdx" : 0,
"doneBy" : "System"
}
]
It should return all documents that have a list item qualified by the first one in this list. Even though the second list item has "bad", it should return this collection doc with "myList" having these 2 list items.
I figured out that a search for one of the fields would be this but how to do both , I'm not sure of the syntax.
db.getCollection('[collection]').find({myList : { $elemMatch : { "status" : "good" }}})
I believe I found it:
db.getCollection('[collection]')find({ myList:
{ $all: [
{$elemMatch : { "status" : "good" }},
{$elemMatch : {"doneBy" : "System"}}
]
} })

Query on 2 properties of the same embedded object in an array

I have a Collection of objects :
db.coll.find().pretty();
{
"_id" : "1",
"elements" : [
{ "key" : "A", "value" : 10 },
{ "key" : "B", "value" : 1 },
]
},
{
"_id" : "2",
"elements" : [
{ "key" : "A", "value" : 1 },
{ "key" : "C", "value" : 33 },
]
}
I want to find the documents that contain an element with "key" equal to "A" AND its "value" is greater than 5.
Not sure if this is possible without using the aggregation framework.
Without aggregation, using $elemMatch and query will be as below :
db.coll.find({"elements":{"$elemMatch":{"$and":[{"key":"A"},{"value":{"$gt":5}}]}}}).pretty()
or if you want use aggregation then used following query for aggregation
db.coll.aggregate({"$unwind":"$elements"},{"$match":{"$and":[{"elements.key":"A"},{"elements.value":{"$gt":5}}]}}).pretty()
Here is the query
db.coll.find({elements: {$elemMatch: {key: "A", value: {$gt: 5}}}});
It uses $elemMatch operator. The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria. (Refer the documentation)

What's the $unwind operator in MongoDB?

This is my first day with MongoDB so please go easy with me :)
I can't understand the $unwind operator, maybe because English is not my native language.
db.article.aggregate(
{ $project : {
author : 1 ,
title : 1 ,
tags : 1
}},
{ $unwind : "$tags" }
);
The project operator is something I can understand, I suppose (it's like SELECT, isn't it?). But then, $unwind (citing) returns one document for every member of the unwound array within every source document.
Is this like a JOIN? If yes, how the result of $project (with _id, author, title and tags fields) can be compared with the tags array?
NOTE: I've taken the example from MongoDB website, I don't know the structure of tags array. I think it's a simple array of tag names.
The thing to remember is that MongoDB employs an "NoSQL" approach to data storage, so perish the thoughts of selects, joins, etc. from your mind. The way that it stores your data is in the form of documents and collections, which allows for a dynamic means of adding and obtaining the data from your storage locations.
That being said, in order to understand the concept behind the $unwind parameter, you first must understand what the use case that you are trying to quote is saying. The example document from mongodb.org is as follows:
{
title : "this is my title" ,
author : "bob" ,
posted : new Date () ,
pageViews : 5 ,
tags : [ "fun" , "good" , "fun" ] ,
comments : [
{ author :"joe" , text : "this is cool" } ,
{ author :"sam" , text : "this is bad" }
],
other : { foo : 5 }
}
Notice how tags is actually an array of 3 items, in this case being "fun", "good" and "fun".
What $unwind does is allow you to peel off a document for each element and returns that resulting document.
To think of this in a classical approach, it would be the equivilent of "for each item in the tags array, return a document with only that item".
Thus, the result of running the following:
db.article.aggregate(
{ $project : {
author : 1 ,
title : 1 ,
tags : 1
}},
{ $unwind : "$tags" }
);
would return the following documents:
{
"result" : [
{
"_id" : ObjectId("4e6e4ef557b77501a49233f6"),
"title" : "this is my title",
"author" : "bob",
"tags" : "fun"
},
{
"_id" : ObjectId("4e6e4ef557b77501a49233f6"),
"title" : "this is my title",
"author" : "bob",
"tags" : "good"
},
{
"_id" : ObjectId("4e6e4ef557b77501a49233f6"),
"title" : "this is my title",
"author" : "bob",
"tags" : "fun"
}
],
"OK" : 1
}
Notice that the only thing changing in the result array is what is being returned in the tags value. If you need an additional reference on how this works, I've included a link here.
$unwind duplicates each document in the pipeline, once per array element.
So if your input pipeline contains one article doc with two elements in tags, {$unwind: '$tags'} would transform the pipeline to be two article docs that are the same except for the tags field. In the first doc, tags would contain the first element from the original doc's array, and in the second doc, tags would contain the second element.
consider the below example to understand this
Data in a collection
{
"_id" : 1,
"shirt" : "Half Sleeve",
"sizes" : [
"medium",
"XL",
"free"
]
}
Query -- db.test1.aggregate( [ { $unwind : "$sizes" } ] );
output
{ "_id" : 1, "shirt" : "Half Sleeve", "sizes" : "medium" }
{ "_id" : 1, "shirt" : "Half Sleeve", "sizes" : "XL" }
{ "_id" : 1, "shirt" : "Half Sleeve", "sizes" : "free" }
As per mongodb official documentation :
$unwind Deconstructs an array field from the input documents to output a document for each element. Each output document is the input document with the value of the array field replaced by the element.
Explanation through basic example :
A collection inventory has the following documents:
{ "_id" : 1, "item" : "ABC", "sizes": [ "S", "M", "L"] }
{ "_id" : 2, "item" : "EFG", "sizes" : [ ] }
{ "_id" : 3, "item" : "IJK", "sizes": "M" }
{ "_id" : 4, "item" : "LMN" }
{ "_id" : 5, "item" : "XYZ", "sizes" : null }
The following $unwind operations are equivalent and return a document for each element in the sizes field. If the sizes field does not resolve to an array but is not missing, null, or an empty array, $unwind treats the non-array operand as a single element array.
db.inventory.aggregate( [ { $unwind: "$sizes" } ] )
or
db.inventory.aggregate( [ { $unwind: { path: "$sizes" } } ]
Above query output :
{ "_id" : 1, "item" : "ABC", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC", "sizes" : "L" }
{ "_id" : 3, "item" : "IJK", "sizes" : "M" }
Why is it needed?
$unwind is very useful while performing aggregation. it breaks complex/nested document into simple document before performaing various operation like sorting, searcing etc.
To know more about $unwind :
https://docs.mongodb.com/manual/reference/operator/aggregation/unwind/
To know more about aggregation :
https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/
Let me explain in a way corelated to RDBMS way. This is the statement:
db.article.aggregate(
{ $project : {
author : 1 ,
title : 1 ,
tags : 1
}},
{ $unwind : "$tags" }
);
to apply to the document / record:
{
title : "this is my title" ,
author : "bob" ,
posted : new Date () ,
pageViews : 5 ,
tags : [ "fun" , "good" , "fun" ] ,
comments : [
{ author :"joe" , text : "this is cool" } ,
{ author :"sam" , text : "this is bad" }
],
other : { foo : 5 }
}
The $project / Select simply returns these field/columns as
SELECT author, title, tags FROM article
Next is the fun part of Mongo, consider this array tags : [ "fun" , "good" , "fun" ] as another related table (can't be a lookup/reference table because values has some duplication) named "tags". Remember SELECT generally produces things vertical, so unwind the "tags" is to split() vertically into table "tags".
The end result of $project + $unwind:
Translate the output to JSON:
{ "author": "bob", "title": "this is my title", "tags": "fun"},
{ "author": "bob", "title": "this is my title", "tags": "good"},
{ "author": "bob", "title": "this is my title", "tags": "fun"}
Because we didn't tell Mongo to omit "_id" field, so it's auto-added.
The key is to make it table-like to perform aggregation.