Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increment key based on another field #4

Open
iamchathu opened this issue Apr 6, 2020 · 6 comments
Open

Increment key based on another field #4

iamchathu opened this issue Apr 6, 2020 · 6 comments
Labels
feature Adds a new Feature or Request info needed Extra information is needed

Comments

@iamchathu
Copy link

Let's say we have Incremented Value(ref) in the document which need to be incremented based on another field(shopId)

{
   "ref": 12,
   "shopId":1
},
{
   "ref": 13,
   "shopId":1
},
{
   "ref": 1,
   "shopId":2
},
{
   "ref": 2,
   "shopId":2
},
{
   "ref": 14,
   "shopId":1
}

What kind of approach can we have? ( the two properties are compound index)

@hasezoey
Copy link
Member

with this plugin currently not possible
(increment value based on other value)

@hasezoey hasezoey added the feature Adds a new Feature or Request label Apr 19, 2020
@tiennguyen1203
Copy link

Any update on this?

@hasezoey
Copy link
Member

like it was said earlier in the comments, it is currently not possible.

if you want it yourself, either implement it and send a PR or better explain what is necessary / wanted.

to summarize (on my understanding), you would need a external (like AutoIncrementID) tracker for the base number, but increment it by another field before applying, tracker + field = newid; or am i misunderstanding something?

@hasezoey hasezoey added the info needed Extra information is needed label Jan 16, 2024
@MxBlu
Copy link

MxBlu commented Nov 5, 2024

I just happen to come across this old issue, which I happened to have solved myself a few years back (if I'm interpreting the problem correctly).

If the issue is that you want a separate counter in the same collection based on the value of another field, check out the change I added to a (very) old version of auto-increment, based on the original mongoose-auto-increment: v0.9.0...MxBlu:auto-increment:master
Just look at the changes inside the src folder.

Taking a cursory glance at the latest version of this repo, my changes should still work. I'm not interested in maintaining my old repo but anyone is free to take these changes for themslves.

@hasezoey
Copy link
Member

hasezoey commented Nov 6, 2024

@MxBlu if i understand your code correctly, that change will allow you to have a different counter document depending on the value of another field, ie:

// a document for shop "a"
{
  for_shop: "a",
}
// would use tracker
{
  field: "_id",
  modelName: "SomeModel",
  count: 10,
  reference_values: {
    for_shop: "a"
  }
}

// where as a document for shop "b"
{
  for_shop: "b"
}
// would use a different tracker document
{
  field: "_id",
  modelName: "SomeModel",
  count: 5,
  reference_values: {
    for_shop: "b"
  }
}
// for the same model

if it this is wanted, it could easily be added (thanks to your code), though i am still not fully sure if this is what OP wanted out of this.

@MxBlu
Copy link

MxBlu commented Nov 6, 2024

Exactly yea, and agreed.

I'm just leaving this here for anyone that stumbles upon it in the future, since the issue description happened to be how I described my problem too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Adds a new Feature or Request info needed Extra information is needed
Projects
None yet
Development

No branches or pull requests

4 participants