2

I have a schema through mongoose:

const mongoose = require('mongoose');

const recipeSchema = mongoose.Schema({

title: String,
chef: String,
updated: {type: Date, default: Date.now},
region: String,
ingredients: [String],
instructions: [String]
}, { collection: 'recipes' })

module.exports = mongoose.model('Recipes', recipeSchema);

I find the mongoose docs really difficult to understand. I am trying to search for a match of all substring within the 'ingredients' array. I read somewhere that it could be done like so:

 .find({ingredients: 'ing1'}) // not working

 .find({'ing1': {$in: ingredients}})  // not working

I find it pretty difficult to find in depth tutorials on mongoose as well. Im thinking about not using it at all anymore and just sticking to mongodb shell.

4 Answers 4

1

You can use a regex search to match substrings:

.find({ingredients: /ing1/})
Sign up to request clarification or add additional context in comments.

1 Comment

Thats perfect! Thank you.
0

The reason that you use mongoose is for testability.

Instead of having to work with a MongoDb instance, which, in Windows can be a pain with the .lock file and the service, mongoose creates the schema that you can test your code with.

The mongoose way is ideal for TDD/TFD.

Below is the model and the mocha test:

recipemodel.js 
    var mongoose = require('mongoose'),Schema=mongoose.Schema;
    var RecipeSchema = new mongoose.Schema({});
    RecipeSchema.statics.create = function (params, callback) {
   '\\ params is any schema that you pass from the test below
      var recipe = new RecipeSchema(params);
      recipe.save(function(err, result) {
        callback(err, result);
      });
      return recipe;
    };
    var recipemodel=mongoose.model('Model', RecipeSchema);
    module.exports = recipemodel;

You don't need to describe the schema, mongoose will create it for you when you pass the values of the collection from a mocha test, for example!

The mocha test is below:

  var mongooseMock = require('mongoose-mock'),
  proxyquire = require('proxyquire'),
  chai = require('chai'),
  expect = chai.expect,
  sinon = require('sinon'),
  sinonChai = require("sinon-chai");
  chai.use(sinonChai);

  describe('Mocksaving a recipe ingredient', function () { 
    var Recipe;
  beforeEach(function () {
    Recipe = proxyquire('./recipemodel', {'mongoose': mongooseMock});
  });

  it('checks if ingredient '+'ing1' + ' saved to mongoose schema', function 
  (done) {
    var callback = sinon.spy();
    var recipe = Recipe.create({ title: "faasos", chef: 
    'faasos',region:'Chennai',ingredients:'ing1',instructions:'abc' }, 
    callback);
    expect(recipe.save).calledOnce;
    expect(recipe.ingredients).equals('ing341');
    done();
    });    
});

mocha test result

The call to a sinon spy is simply to ensure that the call to the data in the schema got saved (mock saved!) and that the 'save' method did get called at least once. This logic flow is in sync with your actual logic, as you would use in code, when the save on a mongodb collection would be made.

Simply change the value to 'ing1' to make the test pass when you run the test.

For an array type, pass the values as below:

var recipe = Recipe.create({ title: "faasos", chef: 
'faasos',region:'Chennai',ingredients:'ing341,ing1',instructions:'abc' }, callback);
    expect(recipe.save).calledOnce;
expect(recipe.ingredients).to.include('ing1');

mocha test for values passed to schema as an array

Comments

0

Try this:

.ingredients.find((i) => i === "ing1")

for all elements in the ingredients array, it looks if the content, here a string element, is strictly equal to "ing1"

1 Comment

You should describe your answer how it works for the future it is because an answer is also read by the others not only by the OP.
0

you can use this way

.find({ingredients: { $in: [yourIngredients] }});

1 Comment

Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, can you edit your answer to include an explanation of what you're doing and why you believe it is the best approach?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.