Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

MongoDb 中如何获取到所有 Collection 的所有数据?

目前的情况:

我有 5 个 Collection, 各个 Collection 中的数据格式大致是相似的, 但是有些许不同, 如下例:

//    猫咪 Collection
{ "info": { "class": "猫咪", "age": "3岁", "name": "咪咪", "owner": "小明" }, "likes": { "food": "鱼", "toys": "逗猫棒" } }
{ "info": { "class": "猫咪", "age": "6岁", "name": "喵喵", "owner": "小红" }, "likes": { "food": "鸡", "toys": "数据线" } }

//    猴子 Collection
{"info":{"class":"猴子","age":"10岁", "from":"北京动物园"},"likes":{"food":"桃子"}}
{"info":{"class":"猴子","age":"5岁", "from":"上海动物园"},"likes":{"food":"香蕉"}}

我的需求:

把这 5 个 Collection 中所有的数据都拿到

我的做法:

将每个 Collection 都获取一遍, 再拼接起来

我的问题:

  1. 有没有一个官方给出的方法, 能做一个所有 Collection 的总集合, 某个 Collection 增删改查的时候, 这个总集合也会相应的增删改查(比如不管是给猫咪集合添加猫咪 还是给猴子集合添加猴子, 都会在总 Collection 上同步添加)
  2. 我是否有必要将这些数据分为 5 个 Collection? 直接放在同一个 Collection 里会更好吗?

编程新手, 希望能够得到解答, 非常感谢各位!!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

看了你示例的两个集合数据格式,可以直接存一个集合呀。不需要分多个集合存储的吧。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...