MongoDB中的多对多 - 我如何引用另一个文档的子目录

时间:2017-12-15 19:30:53

标签: javascript node.js mongodb mongoose mongoose-schema

我试图找到一种通过昵称来表示用户之间关系的方法。

每个用户都有一个或多个昵称,以及零个或多个联系人。例如,有

昵称的用户A:Banana和Kiwi

用户B的昵称:Apple,Pear,Cherry。

A有联系方式:     {         underNickname:香蕉,         toNickname:Apple     }

B有联系方式:     {         underNickname:Apple,         toNickname:香蕉     }

我有一个User模式,其中包含一个Contact集合的子集:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:gravity="center_horizontal"
tools:context="com.example.standard.quizzza.MainActivity">

<TextView
    android:id="@+id/frage"
    android:layout_width="wrap_content"
    android:layout_height="60sp"
    android:layout_marginBottom="5dp"
    android:text="Hello World!"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="110dp"
    android:gravvity="center">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/antwortA"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="Antwort A" />

        <Button
            android:id="@+id/antwortB"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="Antwort B" />
    </TableRow>

    <TableRow
        android:layout_width="150dp"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/antwortC"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="Antwort C" />

        <Button
            android:id="@+id/antwortD"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="Antwort D" />
    </TableRow>

</TableLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="136dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/progressBar"
        android:background="@android:color/transparent"
        android:gravity="center"
        android:text="Frage 1 von 8" />

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="8"
        android:progress="1" />


</LinearLayout>
</LinearLayout>

所以问题是,如何在用户模式中定义这种关系,以便其联系人引用自身的昵称(underNickname)和其他用户(toNickname)

1 个答案:

答案 0 :(得分:0)

我到目前为止找到的最佳选择是使用填充方法:http://mongoosejs.com/docs/populate.html

相关问题