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

set() does not create a nested structure #10

Open
olastor opened this issue May 15, 2021 · 4 comments
Open

set() does not create a nested structure #10

olastor opened this issue May 15, 2021 · 4 comments

Comments

@olastor
Copy link

olastor commented May 15, 2021

Perhaps this is expected behaviour, but I do not understand why this does not work:

const obj = {};
mpath.set('a.b', 3, obj);
console.log(obj); // => {}

It returns an empty object {}, while I was expecting it to return { a: { b: 3 } }. Using .get() it works the other way around:

const obj = { a: { b: 3 } };
console.log(mpath.get('a.b', obj)); // => 3
@chetverikov
Copy link

chetverikov commented Aug 31, 2021

@olastor Yeah, it looks like a bug because mpath is an implementation of a MongoDB-like path notation and MongoDB works as you expect.
@vkarpov15 Are there any similar bugs in mongoose?

@Uzlopak
Copy link
Contributor

Uzlopak commented Jan 19, 2022

What is the actual behavior if you do a

mpath.set('a.3.3', 3, obj);

I mean it could be
{"a": { "3": { "3": 3}}}
or
{ "a": [ undefined, undefined, [ undefined, undefined, 3]]}
or
...

@vkarpov15
Copy link
Member

@Uzlopak I think mpath should err on the side of consistency with the MongoDB server. So the first one you listed is the "correct" behavior.

MongoDB Enterprise > db.test.insertOne({})
{
	"acknowledged" : true,
	"insertedId" : ObjectId("61ea0ffcf42ebc54036f1abc")
}
MongoDB Enterprise > db.test.count()
1
MongoDB Enterprise > db.test.updateOne({}, { $set: { 'a.3.3': 3 } })
{ "acknowledged" : true, "matchedCount" : 1, "modifiedCount" : 1 }
MongoDB Enterprise > db.test.findOne()
{
	"_id" : ObjectId("61ea0ffcf42ebc54036f1abc"),
	"a" : {
		"3" : {
			"3" : 3
		}
	}
}
MongoDB Enterprise > 

But anyway, I ran into this limitation recently with Mongoose as well. I'll open an issue to track this in Mongoose

@Uzlopak
Copy link
Contributor

Uzlopak commented Jan 23, 2022

@vkarpov15
could be solved like this...
Uzlopak@1e3b431

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants