Comments (17)
There's no ability to save sub-models, but you can save document with nested fields. Is that what you want?
from mongorito.
Well let's say I have a domain model. And within domain model I have an array of server documents which I want to have a model for. Very much like mongoose child schema models.
from mongorito.
No, this feature is not supported currently.
from mongorito.
I actually just realized the mongoose does not allowed for subjdocument child schema models. They do allow subdocument arrays of models though:
eg:
Not Allowed:
parentSchema = new Schema({
attr: 'hi',
child: childSchema // not allowed
children: [childSchema] //allowed
})
from mongorito.
👍 -- I was thinking of switching over from Mongoose to Mongorito but I can't without this feature.
from mongorito.
Is this possible yet?
from mongorito.
No, not yet. If this functionality is critical for you, I recommend going with Mongoose, because I'm very short on time lately, so I can't promise any ETA.
from mongorito.
@vdemedes I guess I can do a pull request with some of the code I have come up with.
I have one question you might be able to help with, how would you go about figuring out where the sub document class is. With Doc.get('sub').get('test')
you can load the entity into the parent class because it is already imported; But do you have any idea how you might figure out what class to load the sub into?
Cheers
from mongorito.
Actually i guess it could be done with module.parent
however not the best solution... It would require modules never change their directory
from mongorito.
Something like:
https://github.com/Alex-iFactory/eden/blob/master/bin/bundles/core/model.js
May work, not sure how to make the getAttributes more efficient?
from mongorito.
Not sure I understand your question, could you clarify?
from mongorito.
If we were to allow sub-documents; wouldn't when we run a query on a parent document we need to require the sub-document class:
class parentDocument extends mongorito.Model {
}
class childDocument extends mongorito.Model {
}
let ParentDocument = new parentDocument({
'child' : yield new childDocument({
'title' : 'test'
}).save();
});
In the above case, finding the parent document would return information about the child document, but I am unsure how to use this information to require and load the childDocument
class. What would you use to store the location of the childDocument
class in the parentDocument
document?
Cheers
from mongorito.
Oh I see now. We could modify Query#populate()
to convert inline data to models straight away. Right now populate()
works just like in Mongoose. Given this document:
{
"comments": [1, 2, 3]
}
where [1, 2, 3]
are ObjectIDs, we can find documents with those ids and create a Comment
model for each of them:
let post = await Post.populate('comments', Comment).findOne();
// post.comments now is an array of Comment models
So we could modify implementation of .populate()
to avoid find
operation and create a new model straight away, if current array item is not an ObjectID:
if (isObjectId(item)) {
return model.findById(item);
} else {
return new model(item);
}
Let me know if that's unclear ;)
from mongorito.
That works, I will put together a pull request this week.
Cheers
from mongorito.
Cool!
from mongorito.
is this merged yet?
from mongorito.
Nope, it was never implemented. Hope to implement this in the next major version - #138.
from mongorito.
Related Issues (20)
- Bug with empty fields after using 'Model.embeds()'
- Bug when setting any field after using 'Model.embeds()'
- Bulk insert HOT 3
- How would I handle gridFS with mongorito? HOT 1
- Mass assigment (fillable & guarded fields) HOT 1
- Access document's fields without using .get/.set HOT 3
- outdated website information HOT 1
- How to use inherited(?) mquery findOneAndUpdate method HOT 3
- toJS method HOT 4
- Type definition for intellisense HOT 1
- TypeError: this.Model.query(...).then is not a function HOT 3
- Alterative to old middleware `create` hooks example HOT 1
- Database connect on hold
- support for hashed indexes, and compound indexes HOT 2
- found 1 low severity vulnerability HOT 1
- How to use findOneAndUpdate with option upsert : true ?
- Enormous difference between Github docs and Site docs HOT 1
- abandoned? HOT 1
- How to check the current state of the connection with the database?
- can update the internal mongodb driver
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongorito.