mongodb-query

How to use $regex inside $or as an Aggregation Expression

天大地大妈咪最大 提交于 2019-12-04 14:11:05
I have a query which allows the user to filter by some string field using a format that looks like: "Where description of the latest inspection is any of: foo or bar " . This works great with the following query: db.getCollection('permits').find({ '$expr': { '$let': { vars: { latestInspection: { '$arrayElemAt': ['$inspections', { '$indexOfArray': ['$inspections.inspectionDate', { '$max': '$inspections.inspectionDate' }] }] } }, in: { '$in': ['$$latestInspection.description', ['Fire inspection on property', 'Health inspection']] } } } }) What I want is for the user to be able to use wildcards

Inheritance in MongoDb: how to request instances of defined type

痞子三分冷 提交于 2019-12-04 13:40:08
问题 This is how I used to utilize inheritance in Entity Framework (POCO): ctx.Animals // base class instances (all instances) ctx.Animals.OfType<Cat> // inherited class Cat's instances only ctx.Animals.OfType<Dog> // inherited class Dog's instances only This is the only similar way I found in MongoDb (MongoDb reference): var query = Query.EQ("_t", "Cat"); var cursor = collection.FindAs<Animal>(query); Note in the latter case I have to deal with discriminator ("_t") and hardcode my class name,

Filter results by the Last Array Entry Field Value

拈花ヽ惹草 提交于 2019-12-04 12:29:48
Having this document structure (omitting irrelevant fields for brevity): [ { "_id" : 0, "partn" : [ { "date" : ISODate("2015-07-28T00:59:14.963Z"), "is_partner" : true }, { "date" : ISODate("2015-07-28T01:00:32.771Z"), "is_partner" : false }, { "date" : ISODate("2015-07-28T01:15:29.916Z"), "is_partner" : true }, { "date" : ISODate("2015-08-05T13:48:07.035Z"), "is_partner" : false }, { "date" : ISODate("2015-08-05T13:50:56.482Z"), "is_partner" : true } ] }, { "_id" : 149, "partn" : [ { "date" : ISODate("2015-07-30T12:42:18.894Z"), "is_partner" : true }, { "date" : ISODate("2015-07-31T00:01:51

Find and Replace Strings in Documents Efficiently

久未见 提交于 2019-12-04 12:22:28
问题 I have the following query, to find   tags in a name field and replace them with an empty space - to get rid of them. Name strings can have 1 to many   tags e.g. AA aa AA  aa AA   aa AA    aa AA AA aaaaaaaa ... like that. db.tests.find({'name':/.* .*/}).forEach(function(test){ test.name = test.name.replace(" ",""); db.tests.save(test); }); db.tests.find({'name':/.*  .*/}).forEach(function(test){ test.name = test.name.replace("  ",""); db.tests.save(test); }); db.tests.find({'name':/.*   .*/})

Spring data MongoDB adding arrays to an existing document

安稳与你 提交于 2019-12-04 12:15:28
Say I have the following Collections public @Data class Customer { @Id private String id; private String firstName; private String lastName; @DBRef private List<Address> addressList= new ArrayList<Address>(); } and public @Data class Address { @Id private String id; private String address; private String type; private String customerID; } And each Customer has multiple addresses, and I have implemented MongoRepository . Saving customer for the First time is working pretty well customerRepo.save(customerObject) and before calling the save I am persisting multiple Address Objects and then

MongoDB Geospacial Query Spheres Overlapping Single Point

烂漫一生 提交于 2019-12-04 12:08:43
问题 I am trying to create a geospacial query in MongoDB that finds all circles (with varying radius) that overlap a single point. My data looks something like this: { name: "Pizza Hut", lat: <latitude> lon: <longitude> radius: 20 ... } Basically, I am trying to do exactly what is described in this SO post but with MongoDB - Get all points(circles with radius), that overlap given point geoIntersects (http://docs.mongodb.org/manual/reference/operator/query/geoIntersects/) looks like what I need.

Optimal Compound Indexes for $exists : true (sparse indexes)

谁说我不能喝 提交于 2019-12-04 11:44:07
Problem I need to speedup this kind of query: db.col.find({ a: "foobar", b: { $exists: true} }); Data Distribution Existence of fields: The field a exists in all documents, The field b exists only in ~10% of them. Current Table Stats: db.col.count() // 1,050,505 db.col.count({ a : "foobar" }) // 517.967 db.col.count({ a : "foobar", b : { $exists: true} }) // 44.922 db.col.count({ b : { $exists: true} }) // 88.981 Futrue data growth: So far two batches where loaded (2x around 500,000). Each month another batch of ~500,000 documents will be added. The a field is the name of this batch. Those

how to avoid $push-ing nulls in mongo aggregation framework

橙三吉。 提交于 2019-12-04 11:21:06
问题 $push is aggregating nulls if the field is not present. I would like to avoid this. Is there a way to make a sub expression for $push operator in such way that null values will be skipped and not pushed into the resulting array ? 回答1: Bit late to the party, but.. I wanted to do the same thing, and found that I could accomplish it with an expression like this: // Pushes events only if they have the value 'A' "events": { "$push": { "$cond": [ { "$eq": [ "$event", "A" ] }, "A", "$noval" ] } }

How to optimize MongoDB query with both $gt and $lte?

删除回忆录丶 提交于 2019-12-04 11:01:00
问题 I have the following query that is kind of like a reverse range lookup: db.ip_ranges.find({ $and: [{ start_ip_num: { $lte: 1204135028 } }, { end_ip_num: { $gt: 1204135028 } }] }) When run with only the $lte identifier, the query returns right away. But when I run with both the $gt and $lte in the same query, it is extremely slow (in seconds). Both the start_ip_num and end_ip_num fields are indexed. How can I go about optimizing this query? EDIT I get the following when I use the explain()

How to validate in Mongoose an array and the same time its elements

人盡茶涼 提交于 2019-12-04 10:58:08
I have this schema where I validated the elements of array book , but I don't know how to validate the array itself. var DictionarySchema = new Schema({ book: [ { 1: { type: String, required: true }, 2: String, 3: String, c: String, p: String, r: String } ] }); For example, I would like to put the book array as required. Any help? You can use a custom validator to do this. Simply check that the array itself is not empty: var mongoose = require('mongoose'), Schema = mongoose.Schema; mongoose.connect('mongodb://localhost/test'); var bookSchema = new Schema({ 1: { type: String, required: true },