Changes in the objectID which is unique.1
Hi,
This is the basic suggestion from my side.we have a unique "_id" in each document while inserting the data can we make any changes in your code for example in our company we have one scenario while working on the aggregation query they need auto-generated value in numbers so it is not possible in the MongoDB.
so I thought we can do that thing based on the _id. because we have a unique field then i converted the _id to string and separated the numbers and alphabetic separately but we don't have unique values the after separating the numbers from the objectId.
can we do anything that generation in the objectId based on the numbers or can we have anything like alphabetic have separate numbers like using the let variable like what we did for getting the months and days.
if you do any one of them we can do auto-generation which is unique in numbers for every document either changing in the generation in objectId or adding the alphabetic to numbers like months and days
I am sending the query that I have used in my organization for testing purposes but the result is unexpected so can you guide me on the correct to do it or any changes can you do in the upcoming release it can be appreciated. this is the basic suggestion from my side.I am thinking why don't we do it in the MongoDB other than going to python for random values.
mongodb: For random numbers to generate based on the _id:
If you insert the sample of 1k documents and test this query you will duplicates in the numbers and the count will also decrease.
1st Method: [
{
$addFields: {
numbers: {
$regexFindAll: {
input: {
$toString: "$id",
},
regex: "[0-9]+",
},
},
alpha: {
$regexFindAll: {
input: {
$toString: "$id",
},
regex: "[a-z]+",
},
},
},
},
{
$project: {
numbers: {
$reduce: {
input: "$numbers.match",
initialValue: "",
in: {
$concat: ["$$value", "$$this"],
},
},
},
},
},
{
$group: {
_id: "$numbers",
},
},
{
$count: "string",
},
]
2nd Method:
{
$let: {
vars: {
monthsInString: [
"",
"JAN",
"FEB",
"MAR",
"APR",
"MAY",
"JUN",
"JUL",
"SEP",
"OCT",
"NOV",
"DEC",
],
},
in: {
$arrayElemAt: [
"$$monthsInString",
{
$toInt: {
$dateToString: {
format: "%m",
date: "$dtCollected",
},
},
},
],
},
},
},
Like this can you do for alphabetics so that we can convert the alphabetic to numbers then we can have random values which are unique in numbers .
Can you please check and let me know if any changes I have done wrong from my side or any other way to generate the random values in numbers in the project stage.
Why don't we do in mongodb rather than python.
can you please add this secenrio