Typeorm: Relations works incorrect in Mongo

Created on 17 Oct 2017  路  3Comments  路  Source: typeorm/typeorm

I have two simple entities:

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

    @Column()
    name: string;

    @Column()
    age: number;

    @OneToMany(type => Post, post => post.owner, { cascadeInsert: true })
    posts: Post[]
}

@Entity()
class Post {
    @ObjectIdColumn()
    id: ObjectID;

    @Column()
    title: string;

    @Column()
    isPublished: boolean;

    @ManyToOne(type => User, user => user.posts, { cascadeInsert: true })
    owner: User;
}

And when I try to insert it do the db:

(async () => {
    try {
        await createConnection({
            type: "mongodb",
            database: "typeorm",
            entities: [User, Post],
            synchronize: true,
        });

        const userRepository = getRepository(User);
        const postRepository = getRepository(Post);

        const post1 = postRepository.create({
            title: "Post #1",
            isPublished: true,
        });
        const post2 = postRepository.create({
            title: "Post #2",
            isPublished: false,
        });

        const user = userRepository.create({
            name: "with relation",
            age: 23,
            posts: [post1, post2],
        });

        const savedUser = await userRepository.save(user);
        console.dir(savedUser);
        process.exit(0);
    } catch (err) {
        console.error(err);
        process.exit(1);
    }
})();

it insert correct User document but Post has only _id and relation id props:

image

I also tried setting cascadeInsert to false and manually insert posts do db:

const user = userRepository.create({
    name: "with relation manually",
    age: 23,
    posts: await postRepository.save([post1, post2]),
});

const savedUser = await userRepository.save(user);

But the userRepository.save(user) overwrite the document (they still have only _id and ownerId props. Only if I call postRepository.save([post1, post2]) without setting user.posts relations, it's correctly saved in db but without ownerId of course.

The only way to persist it correctly is by inverse side:

const user = userRepository.create({
    name: "mongo relation",
    age: 23,
});
const savedUser = await userRepository.save(user);

const post1 = postRepository.create({
    title: "Post #1",
    isPublished: true,
    owner: savedUser,
});
const post2 = postRepository.create({
    title: "Post #2",
    isPublished: false,
    owner: savedUser,
});
await postRepository.save([post1, post2]);

image

However I still can't load the post->user relation, neither by eager nor by postRepository.find({ relations: ["owner"]}). So I bet that relations aren't yet supported in mongo and I'm trying to use undocumented features? 馃槅

Most helpful comment

How can I use insert many for storing documents in one collection and one column having reference of data in other collections.
Eg; Collection1: Shop
Collection2: Owner
Shop{
id
owner: Owner[]
}

All 3 comments

So I bet that relations aren't yet supported in mongo and I'm trying to use undocumented features?

exactly 馃槈

I'll close it since we already have an issue (#655) for relations in mongo.

How I connect 2 documents

How can I use insert many for storing documents in one collection and one column having reference of data in other collections.
Eg; Collection1: Shop
Collection2: Owner
Shop{
id
owner: Owner[]
}

Was this page helpful?
0 / 5 - 0 ratings