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

Query an Array of Embedded Documents #2483

Closed
CosmoFruit opened this issue Jul 8, 2018 · 12 comments
Closed

Query an Array of Embedded Documents #2483

CosmoFruit opened this issue Jul 8, 2018 · 12 comments

Comments

@CosmoFruit
Copy link

Issue type:

[X] question
[ ] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[X] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[X] latest
[ ] @next
[ ] 0.x.x (or put your version here)

I have two entities:

export class User {
  @ObjectIdColumn()
  id: ObjectID;

  @Column()
  username: string;

  @Column()
  avatar: string;

  @Column(type => UserAccount)
  accounts: UserAccount[];

}

export class UserAccount {
  @ObjectIdColumn()
  id: ObjectID;

  @Index({unique: true})
  @Column()
  email: string;

  @Column()
  password_hash: string;
}

i try find user by account id:

  const u: User = await this.userRepository.findOne({
      where { accounts: {id: targetAccauntId}}
    })

but result is empty

how to right search in array of embedded entities?

@rosshadden
Copy link

I came to ask the same question. Though it's not just arrays, I can't figure out how to query nested objects either (if accounts was an object in your example instead of an array, for instance).

@lvao813
Copy link

lvao813 commented Oct 30, 2018

I also encountered the same problem.

@atejadac
Copy link

I too have the same problem. Pretty hard to believe that TypeOrm doesn't offer that feature. Switching to Mongoose if this is not solved.

@Squallium
Copy link

Hi, I think this another issue could be related #1766

@rustamwin
Copy link
Contributor

@CosmoFruit try it:

const u: User = await this.userRepository.findOne({
       "accounts.id": targetAccauntId
    })

or

const u: User = await this.userRepository.findOne({
       {accounts: {id: targetAccauntId}}
    })

@rustamwin
Copy link
Contributor

See #3756

@kaczor6418
Copy link

@CosmoFruit try it:

const u: User = await this.userRepository.findOne({
       "accounts.id": targetAccauntId
    })

or

const u: User = await this.userRepository.findOne({
       {accounts: {id: targetAccauntId}}
    })

Sorry but both of this options are not working, they are returning only undefined

@MuhammadAbuSufian
Copy link

MuhammadAbuSufian commented Apr 17, 2020

 const u: User = await this.userRepository.findOne({
      where: { "accounts.id" : targetAccauntId}
    })

@imnotjames
Copy link
Contributor

For questions, please check out the community slack or check TypeORM's documentation page on other support avenues - cheers!

@hoangtrung17
Copy link

I tried this code for save data to test but accounts fill is null affter transform:
@Column(type => UserAccount) accounts: UserAccount[];
I think Mongoose is better than typeOrm to work with mongoDB

@adapolytech
Copy link

You can use aggregation it support nested document query and typeorm support aggregation
In the match stage you can specify field of nested document
$match : {
'account.id': id
}

@AbdallahShnaino
Copy link

const u: User = await this.userRepository.findOne({
where: { "accounts.id" : targetAccauntId}
})

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

No branches or pull requests