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

push element issue for a schema with an array of arrays #9429

Closed
RoosterH opened this issue Sep 17, 2020 · 1 comment
Closed

push element issue for a schema with an array of arrays #9429

RoosterH opened this issue Sep 17, 2020 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@RoosterH
Copy link

RoosterH commented Sep 17, 2020

Yes, I am using 5.10.5

Do you want to request a feature or report a bug?
report a bug or maybe I just defined the schema wrongly?

What is the current behavior?
I want to define an array that contains multiple arrays, such as:

runGroupEntries[['userId1', 'userId2', 'userId3'], ['userId4', 'userId5', 'userId6], ...]

Here is my schema:

runGroupEntries: [
        [
            {
                type: mongoose.Types.ObjectId,
                ref: 'User',
                require: true
            }
        ]
    ]

The way I initialize the array:

for (let i = 0; i < numGroups; ++i) {
    event.runGroupEntries.push(undefined);
}

From Atlas, I can see it correctly as:
image

After push the first element to the array using the following:

event.runGroupEntries[runGroup].push(userId);  //runGroup = 0

I got

runGroupEntries: [ [ null, [Array] ], [ null ], [ null ], [ null ], [ null ] ],

image

You can see the pushed "userId" is showing as an array.

After pushing the 2nd element. The array became:

runGroupEntries: [ [ null, 5f5c1d95e4f678ce190d5624, [Array] ], ....]

image

The first pushed element became userId now but the 2nd element is an array.

If the current behavior is a bug, please provide the steps to reproduce.
Please use above lines to reproduce.

What is the expected behavior?
expecting all the elements are "userId" instead of seeing an array for the latest pushed element.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
"mongoose": "^5.10.5",
MongoDB v4.2.6
Node.js v12.16.2

@RoosterH
Copy link
Author

RoosterH commented Sep 19, 2020

Tried something different and found another issue.

I tried to replace the first null element with a real userID:

if (	event.runGroupEntries[runGroupIndex].length === 1 &&
	event.runGroupEntries[runGroupIndex][0] === null
	) {
	event.runGroupEntries[runGroupIndex][0] = userId;
} else {
	event.runGroupEntries[runGroupIndex].push(userId);
}

After re-assignment, I did a print out. The null element indeed been replaced with userId:

 runGroupEntries: [
    [ '5f5c1d95e4f678ce190d5624' ],
    [ null ],
    [ null ],
    [ null ],
    [ null ]
  ],

However, after I did save, I am using transaction BTW

await event.save({ session: session });

The userId is not showing in MongoDB
image

All other information is saved but not the userId in the runGroupEntries.

EDIT:
replaced event.runGroupEntries[runGroupIndex][0] = userId; with

event.runGroupEntries.set(0, userId);

UserId is now been saved to MongoDB but still shows it as an array:
image

@vkarpov15 vkarpov15 added this to the 5.10.7 milestone Sep 22, 2020
@vkarpov15 vkarpov15 added needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Sep 22, 2020
vkarpov15 added a commit that referenced this issue Sep 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

2 participants