mongodb-query

mongodb query on DBRef type

醉酒当歌 提交于 2020-01-05 08:36:22
问题 How do I turn this query in a valid mongodb Query in mongodb shell. { 'cars.owner.$ref' : 'users' } cars.owner is a DBRef here, but $ref is invalid I get this error: "$err" : "Positional operator does not match the query specifier." My objective here is to figure out if there are any cars "owned" by different collections then users. 回答1: MongoDB documentation (database-reference) says following: MongoDB applications use one of two methods for relating documents: Manual references where you

I can not query with like % in collection of phalcon

烈酒焚心 提交于 2020-01-05 06:34:21
问题 I use phalcon with mongo db. Model is Collection . I want use find to get SELECT * FROM Users WHERE Username LIKE '%TienNguyen%' I see in mongo db have query b.users.find( { Username: /TienNguyen/ } ) But in collection of Phalcon. How do I code it. I tried Users::find([['Username' => "/TienNguyen/"]]); It is not working. Please help me 回答1: As was mentioned above, regex searches that are not anchored at the beginning of the string can be very expensive because they require a full table scan.

I can not query with like % in collection of phalcon

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-05 06:33:32
问题 I use phalcon with mongo db. Model is Collection . I want use find to get SELECT * FROM Users WHERE Username LIKE '%TienNguyen%' I see in mongo db have query b.users.find( { Username: /TienNguyen/ } ) But in collection of Phalcon. How do I code it. I tried Users::find([['Username' => "/TienNguyen/"]]); It is not working. Please help me 回答1: As was mentioned above, regex searches that are not anchored at the beginning of the string can be very expensive because they require a full table scan.

Mongo / Mongoose - Aggregating by Date

假装没事ソ 提交于 2020-01-05 05:52:44
问题 I have a mongo/mongoose schema which when queried retruns documents such as { "_id" : ObjectId("5907a5850b459d4fdcdf49ac"), "amount" : -33.3, "name" : "RINGGO", "method" : "VIS", "date" : ISODate("2017-04-26T23:00:00Z"), "importDate" : ISODate("2017-05-01T21:15:49.581Z"), "category" : "Not Set", "__v" : 0 } { "_id" : ObjectId("5907a5850b459d4fdcdf49ba"), "amount" : -61.3, "name" : "Amazon", "method" : "VIS", "date" : ISODate("2017-03-23T00:00:00Z"), "importDate" : ISODate("2017-05-01T21:15:49

MongoDB Java driver : no such cmd: aggregate

北慕城南 提交于 2020-01-05 05:50:10
问题 I am calling the MongoDB aggregate function in my code as : AggregationOutput output = collection.aggregate( matchUserID, unwindF, matchFUsers,projection); I have tested my code in my localhost, and it works perfect. When I am using the same in another DB (version 2.2.1), it gives this error : com.mongodb.CommandResult$CommandFailure: command failed [aggregate]: { "serverUsed" : "<server address>" , "errmsg" : "no such cmd: aggregate" , "bad cmd" : { "aggregate" : .... } Any clue why ? 回答1:

Which of the following queries will use the index?

风流意气都作罢 提交于 2020-01-05 04:19:07
问题 Given collection foo with the following index: db.foo.createIndex( { a : 1, b : 1, c : 1 } ) We need to select which of the following queries will use the index? db.foo.find( { c : 1 } ).sort( { a : -1, b : 1 } ) db.foo.find( { b : 3, c : 4 } ) db.foo.find( { a : 3 } ) db.foo.find( { c : 1 } ).sort( { a : 1, b : 1 } ) I'm surprised that 3 , 4 are correct options and 1 , 2 are not. Why is the following queries will use the index? db.foo.find( { a : 3 } ) This seems to be able to use the index

MongoDB Mongoose querying a deeply nested array of subdocuments by date range

最后都变了- 提交于 2020-01-05 04:18:11
问题 I have a question that is similar to this other question but not exactly the same because my data structure is more deeply nested, and the accepted answer did not resolve the issue. Technologies: MongoDB 3.6, Mongoose 5.5, NodeJS 12 I am trying to query a deeply nested array of objects. The query will accept a "Start Date" and an "End Date" from the user. Item Report is an array of subdocuments that contains another array of subdocuments "Work Done By". All WorkDoneBy objects that have a

How to use $slice a $filter result in MongoDB?

China☆狼群 提交于 2020-01-05 04:00:14
问题 I have a item collection with the following format: { "_id": 123, "items": [{ "name": "item1", "status" : "inactive", "created" : ISODate("2018-02-14T10:39:28.321Z") }, { "name": "item2", "status" : "active", "created" : ISODate("2018-02-14T10:39:28.321Z") }, { "name": "item3", "status" : "active", "created" : ISODate("2018-02-14T10:39:28.321Z") }, { "name": "item4", "status" : "inactive", "created" : ISODate("2018-02-14T10:39:28.321Z") }, { "name": "item5", "status" : "active", "created" :

Insert array where element does not exist else update it ( with multiple conditions )

拜拜、爱过 提交于 2020-01-05 02:37:46
问题 { _id:'1', name:'apple', option:[{ weight:'10', size:'40' price:'40', }, {weight:'40', size:'40' price:'200'}] } If weight or the size is different I want to upsert new object in the array, if both the size and the weight matched I want to update it. How can I do it? 回答1: Similar to your previous question, you use .bulkWrite() but since the array element selection has "multiple conditions" this is where you use $elemMatch: db.collection.bulkWrite([ { "updateOne": { "filter": { "_id": "1",

Geo-query using a circle as area to match at least one of the points of MultiPoint object in MongoDB

梦想的初衷 提交于 2020-01-05 02:36:07
问题 I have the following document in the entities collection at Mongo (a 2dsphere index for location.coords is in place): > db.entities.find({},{location: 1}).pretty() { "_id" : { "id" : "en3", "type" : "t", "servicePath" : "/" }, "location" : { "attrName" : "position", "coords" : { "type" : "MultiPoint", "coordinates" : [ [ -3.691944, 40.418889 ], [ 4.691944, 45.418889 ] ] } } } As far as I have checked, $geoWithin only matches when the geometry includes all the points of the MultiPoint, e.g: >