Coder Social home page Coder Social logo

Comments (3)

srguiwiz avatar srguiwiz commented on June 14, 2024

pull request #110 has been opened

from loopback-connector-mongodb.

fedebalderas avatar fedebalderas commented on June 14, 2024

Hi @srguiwiz any idea about why this query doesn't work?

I'm trying to do a query over a property which is a Date type using elemMatch. Take a look please (I've changed a little my model for this example).

My model
screen shot 2016-05-26 at 11 13 01 am

This query works fine:
screen shot 2016-05-26 at 11 24 17 am

But this doesn't work:
screen shot 2016-05-26 at 11 26 22 am

Or this:
screen shot 2016-05-26 at 11 26 59 am

Any idea?

from loopback-connector-mongodb.

ssh24 avatar ssh24 commented on June 14, 2024

You can use nested specs on the query itself without having to use mongoDB syntaxes.

Here is an sample code snippet:

model-definition.js

{
  "name": "Bank",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "id": {
      "type": "string",
      "required": true,
      "id": true
    },
    "name": {
      "type": "string"
    },
    "opens": {
      "type": "object"
    },
    "closes": {
      "type": "object"
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

boot-script.js

'use strict';
var util = require('util');

module.exports = function(app) {
  var db = app.dataSources.mongoDs;
  var Bank = app.models.Bank;

  var data = [{
    id: '1',
    name: 'TD',
    opens: {
      monday: '7:00am',
      tuesday: '7:00am',
      wednesday: '7:00am',
    },
    closes: {
      monday: '7:00pm',
      tuesday: '7:00pm',
      wednesday: '7:00pm',
    },
  }, {
    id: '2',
    name: 'BMO',
    opens: {
      monday: '9:00am',
      tuesday: '7:00am',
      wednesday: '10:00am',
    },
    closes: {
      monday: '5:00pm',
      tuesday: '7:00pm',
      wednesday: '8:00pm',
    },
  }];

  db.automigrate(function(err) {
    if (err) throw err;
    console.log('\nAutomigrate complete');

    Bank.create(data, function(err, result) {
      if (err) throw err;
      console.log('\nCreated instance: ' + util.inspect(result, 4));

      Bank.find({where: {'opens.monday': '9:00am'}},
      function(err, result) {
        if (err) throw err;
        console.log('\nFind instance: ' + util.inspect(result, 4));
      });
    });
  });
};

The above query results in all the banks that opens on monday at 9:00am.

Hopefully, this solves the issue. Closing this as a result. Feel free to reopen if needed.

from loopback-connector-mongodb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.