I'm a little unclear about indexing in mongodb.
Below I have a schema (in mongoose) for a book entry. It has an owner and a requestee.
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const bookSchema = Schema({
owner : { type: Schema.Types.ObjectId, ref: 'User', index: true },
title : String,
author : String,
image : String,
added : Date,
requestee: { type: Schema.Types.ObjectId, ref: 'User', index: true },
}, {collection: 'books'})
module.exports = mongoose.model("Book", bookSchema);
What I want to do is to be able to query from both perspectives.
For the owner of the book:
Book.find({owner: ObjectId(user._id)})
This will be able to tell me if anyone is requesting the book
For the requestee:
Book.find({requestee: ObjectId(requestee._id)})
This will be able to tell me the requests a requestee has made.
I'm unclear about if what I have above is correct in terms of indexes. Can I have multiple single key indexes, or is what I'm doing scanning every document looking for a match?
Running the queries from the shell seem to work but again don't know if this is correct.
measure index usedoc, I think my assumption is correct based off what I'm seeing in theexplain("executionStats"). I think what you have is a perfectly acceptable answer. If you want to put it as an answer I'll accept it.