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

Mongo, populate nested field only from collection #9078

Closed
borm opened this issue Jan 10, 2021 · 4 comments
Closed

Mongo, populate nested field only from collection #9078

borm opened this issue Jan 10, 2021 · 4 comments
Labels
status: duplicate Is a duplicate of another issue

Comments

@borm
Copy link

borm commented Jan 10, 2021

Related issue, no answer,

@lauriejim why u did close it?

Model

"attributes": {
  "admin": {
    "plugin": "users-permissions",
    "model": "user",
    "via": "restaurant"
  },
  "users": {
    "plugin": "users-permissions",
    "collection": "user",
    "via": "restaurant"
  }
}

Original query

strapi.query('restaurant').findOne({ id: params.id }, populate);

Query 1

populate = [];

Actual result

{
  admin: USER.ID,
  users: []
}

Query 2

populate = ['users'];

Actual result

{
  admin: USER.ID,
  users: [USER, USER, USER]
}

Query 3

populate = ['users.id'];

Actual result

 error MissingSchemaError: Schema hasn't been registered for model "restaurant".

Expected result

{
  admin: USER.ID,
  users: [USER.ID, USER.ID, ...], // not user model, only id
}
  • Strapi Version: 3.4.0
  • Operating System: MacOS
  • Database: MongoDB
  • Node Version: v14.15.3
@derrickmehaffy derrickmehaffy added the status: pending reproduction Waiting for free time to reproduce the issue, or more information label Jan 11, 2021
@derrickmehaffy
Copy link
Member

Alright so going to break this down in steps:

Regarding the closed issue, it was done so because it was attempting to do something we don't support (and had extremely poor formatting, which our rules are quite clear about and you did very well on in this issue)


Query 1

The first one is a limitation of our population and how we handle relations, though this issue says components it also applies to certain relations. So duplicate of: #7360


Query 2

Functions as intended


Query 3

The population array only works on relational fields, so if you had some other relations on the user model, say users had a "comments" and "likes" relations (don't care which type) and you only wanted to populate the "likes"; Your population array would look like: [ "user", "user.like" ], this would leave comments un-populated.

Also in your case I'm guessing you forgot to change the default controller from our docs example (swap out the restaurant for the actual model name)

examples:
image
image


At the moment we don't support dynamic population of fields themselves, only relational ones. We do plan to make a change I believe in Q3 which will make default population as none, and let users pick the fields (not just relational) they want to show.

Marking this as closed, as aside from the duplicate issue I linked, it's working as intended. If you need more advanced population you will have to build a custom query: https://strapi.io/documentation/developer-docs/latest/concepts/queries.html#mongoose

@derrickmehaffy derrickmehaffy added status: duplicate Is a duplicate of another issue and removed status: pending reproduction Waiting for free time to reproduce the issue, or more information labels Jan 11, 2021
@derrickmehaffy
Copy link
Member

This issue has been mentioned on Strapi Community Forum. There might be relevant details there:

https://forum.strapi.io/t/how-to-populate-component-that-comes-from-a-deep-nested-relation/3018/8

@MotaZor94
Copy link

Hi @derrickmehaffy,

I have tried your solution above and it worked perfectly. The only issue then is that all the repeatable components get deleted from the object. Is there a solution to this maybe?

@derrickmehaffy
Copy link
Member

This issue has been mentioned on Strapi Community Forum. There might be relevant details there:

https://forum.strapi.io/t/populate-deleted-components-and-repeatable-components/7459/1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate Is a duplicate of another issue
Projects
None yet
Development

No branches or pull requests

3 participants