1

Say I have a User schema/model and the user has a list of friends. Mongoose wants you to store your list of friends (foreign key / ObjectID type) as an array, right? Which means if I want to find my friend by ID, Mongoose will search the array until it finds the first instance of a friend with the ID I want. That seems really time inefficient no? Is there a better way?

const FriendSchema = new Schema({
  username: { type: String, required: true, unique: true },
});

const UserSchema = new Schema({
  username: { type: String, required: true, unique: true },
  friends: [FriendSchema],
});
Justin Jaeger
  • 181
  • 1
  • 6
  • Does this answer your question? [How To Create Mongoose Schema with Array of Object IDs?](https://stackoverflow.com/questions/22244421/how-to-create-mongoose-schema-with-array-of-object-ids) – Matt Wang Nov 12 '20 at 06:17

2 Answers2

1

Part of what I was looking for is this:

Indexes are what allows you to "iterate" through an array or a field in a collection without having to look at every single one. So to make sure you don't waste time iterating, you can create an "index" on any field and it makes it searchable in a binary-tree structure. https://docs.mongodb.com/manual/indexes/

Arrays already are made to have the field be a "key" so you wouldn't need to worry about the time complexity of searching an array by the field name of one of its elements. https://docs.mongodb.com/manual/core/index-multikey/

Justin Jaeger
  • 181
  • 1
  • 6
0

Use ref to refer to the documents in another schema and call populate to get the referred doc.

// friend.model.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const FriendSchema = new Schema({
  username: { type: String, required: true, unique: true },
});
module.exports = mongoose.model('Friend', FriendSchema);
// user.model.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const UserSchema = new Schema({
  username: { type: String, required: true, unique: true },
  friends: [{ type: Schema.Types.ObjectId, ref: 'Friend' }],
});
module.exports = mongoose.model('User', UserSchema);
const User = require('user.model.js');

User.find(...)
  .populate('friends')
  .exec()
Matt Wang
  • 318
  • 1
  • 10
  • Hey Matt, appreciate the comment! Agreed this is more efficient. I still am wondering though how it might be possible to structure the data so that you can find a friend by ID without mongoose iterating through your entire array (constant vs linear time complexity) – Justin Jaeger Nov 11 '20 at 20:13