Code Monkey home page Code Monkey logo

Comments (5)

louischatriot avatar louischatriot commented on June 23, 2024 1

Currently, you can put objects in arrays in NeDB. You can then query the array field as usual (that is, query it like a normal field and if any element of the array matches there is a match). The cool thing is that NeDB does deep-equality matching for basic queries on objects, so you can write queries like this:

var db = new Datastore({ inMemoryOnly: true });

db.insert({ a: 5, b: [ 'yo', { bloup: 'blip' }, 7 ] }, function (err, newDoc) {
  // First element of the array, a native type string)
  db.findOne({ b: 'yo' }, function (err, doc) {
    console.log(doc);  // Will return newDoc
  });

  // Second element, an object. Deep equality is used
  db.findOne({ b: { bloup: 'blip' } }, function (err, doc) {
    console.log(doc);   // Will also return newDoc
  });

  // You can also use comparator queries
  db.findOne({ b: { $gt: 6 } }, function (err, doc) {
    console.log(doc);   // Will also return newDoc
  });
});

You don't yet have the power to query for the objects in an array, such as elemMatch, and I may implement that in the future but there is still more basic work to do so I'll close this issue in the meantime :)

Also, note that when I never used arrays containing non-primitive types when I used MongoDB for tldr.io. Instead, I used "foreign keys" (storing an array of ids) and used two queries (one for the main document, the other to get the documents in the array). Mongoose made this process easy.

from nedb.

 avatar commented on June 23, 2024

I'm guessing that comparator queries on properties of objects in arrays don't work - probably a bit much to ask but this doesn't seem to

{"key": "A","tags": [{"tag": 1},{"tag": 2},{"tag": 3}]}
{"key": "B","tags": [{"tag": 1},{"tag": 3}]}
{"key": "C","tags": [{"tag": 1},{"tag": 2}]}

db.find({"tags": {"tag": {$gt: 2}}},...

from nedb.

louischatriot avatar louischatriot commented on June 23, 2024

No, but MongoDB doesn't either. That seems like elemMatch to me.

from nedb.

 avatar commented on June 23, 2024

You're probably right there :)

On 21 June 2013 14:06, Louis Chatriot [email protected] wrote:

No, but MongoDB doesn't either. That seems like elemMatch to me.


Reply to this email directly or view it on GitHubhttps://github.com//issues/32#issuecomment-19814618
.

from nedb.

sergeyksv avatar sergeyksv commented on June 23, 2024

MongoDb does searches in array objects and that's one of its benefits.
For your case right query will be: db.find({"tags.tag": {$gt: 2}})

from nedb.

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.