【问题标题】:How to store nested Array-Object into Mongodb?如何将嵌套的 Array-Object 存储到 Mongodb 中?
【发布时间】:2022-11-04 20:10:16
【问题描述】:

我在 MongoDB Atlas 中存储数据时遇到了困难。我想存储的数组是这样的:

Array [
  Array [
    "6352546fb6e1702a96df5931",
    "[{\"date\":\"2022-10-21\"},{\"index\":\"6352546fb6e1702a96df5931\"},{\"setDetails\":[{\"setIndex\":0,\"result\":12,\"weight\":\"12\",\"reps\":0}]},{\"setDetails\":[{\"setIndex\":1,\"result\":25,\"weight\":\"25\",\"reps\":0}]}]",
  ],
  Array [
    "6360db542adb98c1ac717890",
    null,
  ],
  Array [
    "636284882f3e1d9e6808071d",
    "[]",
  ],
  Array [
    "63628afd2f3e1d9e6808071f",
    null,
  ],
  Array [
    "63628b8d2f3e1d9e68080721",
    null,
  ],
  Array [
    "63628bf32f3e1d9e68080723",
    null,
  ],
  Array [
    "63628c092f3e1d9e68080725",
    null,
  ],
  Array [
    "63628cb72f3e1d9e68080727",
    null,
  ],
]

我尝试了各种模式类型,如数组或对象,

const UserWorkoutHistory = {
  workout: Object, // something wrong with this line
  userId: String,
};

const UserWorkoutScheme = mongoose.model(
  'UserWorkoutHistory',
  UserWorkoutHistory
);

module.exports = UserWorkoutScheme;

但是尽管我在后端正确地获得了相同的数据,但数据库中没有存储任何内容。您能否提供线索以找到解决方案?

更新:

我从 AsyncStorage React Native 获得数据:

const GetData = async () => {
    try {
      workoutId.map((item) => {
        //console.log('item', item);
        return AsyncStorage.getItem(item).then((value) => {
          //setData(value);

          const dataArray = JSON.parse(value);
          if (dataArray.length !== 0) {
            // console.log('value', data.length);
            setData((data) => [...data, dataArray]);
          }
        });
      });
    } catch (e) {
      alert(e.message);
    }
  };

然后,只需通过 Axios post 将其发送到服务器:

const SyncData = () => {
    Axios
      .post('http://10.4.1.6:8080/userworkoutdata', {
        data,
        userId,
      })
      .then((res) => {
        //console.log('res', res.data);
      })
      .catch((error) => {
        console.log('error', error);
      });
  };

这就是我在 Express js 的发布路线中得到的

    630749ebb3c79314cc86877c //userId
[
      { date: '2022-10-21' },
      { index: '6352546fb6e1702a96df5931' },
      { setDetails: [ [Object] ] },
      { setDetails: [ [Object] ] }
    ] // data I wanted to store

用户 ID 已正确存储,但数据未正确存储

【问题讨论】:

    标签: javascript node.js arrays mongodb


    【解决方案1】:

    我不确定为什么你拥有的东西对你不起作用,我有一个类似的东西,它对我有用。但是你可以试试

    const UserWorkoutHistory = {
      workout: [[mongoose.Schema.Types.Mixed]], // array of array of mixed
      userId: String,
    };
    

    或者

    const UserWorkoutHistory = {
      workout: {type: Object},
      userId: String,
    };
    

    【讨论】:

    • 谢谢,但是在应用您的 sn-p 代码后,我得到了相同的结果,{ "_id": { "$oid": "6363de8206a8458cee84e812" }, "workout": [], "userId": "630749ebb3c79314cc86877c", " __v": 0 } 存储了一个空数组
    • 你能分享一下你如何存储数据的代码sn-p吗?
    • 我用更多数据更新了我的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多