将2D数组保存到MongoDb的最佳方法 [英] Best way to save 2D array to MongoDb

查看:164
本文介绍了将2D数组保存到MongoDb的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我生成的保存2D数组的最佳方法是什么,问题是因为它不是小数组。因此,有一种方法可以将该2D数组转换为JSON格式或某种格式,然后用猫鼬保存,然后快速将其转换回去,其中的值是字符串,所有想法都值得赞赏。

What is the best way to save 2D array, that I have generated,problem is because it won't be small array. So is there a way to convert this 2D array to some JSON format or something and then save that with mongoose, and then to quickly convert it back, the values in are strings, all ideas all appreciated.

这是我的架构:

var UserRatingSchema = new Schema({
  user: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
  movie: {type: mongoose.Schema.Types.ObjectId, ref: 'Movie'},
  rating: String, 
});

数组示例:

[nothing] Movie1 Movie2

User1 10无评分

User2 6 5

...
如果将每个实例保存到数据库是否有效?

And example of array:
[nothing] Movie1 Movie2
User1 10 No rating
User2 6 5
... Is it efficient if I save every instance to database?

推荐答案

您可以这样定义模型,

location: {type: [Number]}

并在模式索引中定义它,例如

and define this in schema index like

UserSchema.index({location : '2d'});

现在您的结构将是这样

 "location": [
        67.0783412,
        24.9044033
    ]

您还可以通过将键添加到值中来进行修改!

you can also modify by adding keys to the values!

这篇关于将2D数组保存到MongoDb的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆