mongodb-query

mongoDB: Show MinuteBucket in Time value as ending time for the interval

时光怂恿深爱的人放手 提交于 2020-04-16 09:18:47
问题 I have created Mongo PlayGround Currently dateHour field is dividing time by 15min interval. For example - if `"updatedAt": ISODate("2020-03-20T18:16:50.000Z")` it shows as : "dateHour": ISODate("2020-03-20T18:15:00Z"), so First 15min bucket is represented as 18:00:000z (this covers minutes from 1 to 15) second 15min bucket is represented as 18:15:000z (this covers minutes from 15 to 30) and so on.... Expected - First 15min bucket should be displayed as 18:15:000z (this covers minutes from 1

FindAndUpdate How to check if document was really updated

非 Y 不嫁゛ 提交于 2020-04-14 10:39:09
问题 Imagine the following model: var Office = { id: 1, name: "My Office", branches: [ { adddress: "Some street, that avenue", isPrincipal: true, }, { adddress: "Another address", isPrincipal: false, }, ] } I'd like to remove a branch, but we can't let the user remove the principal branch from an office. So here's my function: remove: function(body) { return new Promise(function(resolve, reject) { return Office.findByIdAndUpdate(1, { $pull: {'branches': {_id: body.branch.id}}}, { new: true })

Insert if not exists, else remove MongoDB

限于喜欢 提交于 2020-04-14 09:43:34
问题 So I have a query in MongoDB (2.6.4) where I am trying to implement a simple upvote/downvote mechanism. When a user clicks upvote, I need to do the following: If already upvoted by user, then remove upvote. Else if not upvoted by user, then add upvote AND remove downvote if exists. So far, my query formed (is incorrect) is: db.collection.aggregate([ { $project: { "_id" : ObjectId("53e4d45c198d7811248cefca"), "upvote": { "$cond": [ {"$in": ["$upvote",1] }, {"$pull": {"upvote" : 1}}, {"

Mongoose get docs matching the array

此生再无相见时 提交于 2020-04-10 03:52:33
问题 I have a schema like this { members: [{ type: Schema.Types.ObjectId, ref: 'User' }] createdAt: { type: Date, default: Date.now } ... } And the docs are 1- { members: ["some id 1", "some id 2"] }, createdAt: "someTime ago" 2- { members: ["some id 1", "some id 2", "some id 3"] }, createdAt: "someTime ago" I want to find the docs which match the exact an array of elements ['some id 1', 'some id 2'] ie doc 1 in the above sample docs How this could be possible? Any help would be appreciated. 回答1:

Mongoose get docs matching the array

ⅰ亾dé卋堺 提交于 2020-04-10 03:51:10
问题 I have a schema like this { members: [{ type: Schema.Types.ObjectId, ref: 'User' }] createdAt: { type: Date, default: Date.now } ... } And the docs are 1- { members: ["some id 1", "some id 2"] }, createdAt: "someTime ago" 2- { members: ["some id 1", "some id 2", "some id 3"] }, createdAt: "someTime ago" I want to find the docs which match the exact an array of elements ['some id 1', 'some id 2'] ie doc 1 in the above sample docs How this could be possible? Any help would be appreciated. 回答1:

Mongoose get docs matching the array

青春壹個敷衍的年華 提交于 2020-04-10 03:51:05
问题 I have a schema like this { members: [{ type: Schema.Types.ObjectId, ref: 'User' }] createdAt: { type: Date, default: Date.now } ... } And the docs are 1- { members: ["some id 1", "some id 2"] }, createdAt: "someTime ago" 2- { members: ["some id 1", "some id 2", "some id 3"] }, createdAt: "someTime ago" I want to find the docs which match the exact an array of elements ['some id 1', 'some id 2'] ie doc 1 in the above sample docs How this could be possible? Any help would be appreciated. 回答1:

Spring Data mongo case insensitive like query

自作多情 提交于 2020-04-08 09:23:25
问题 I want to make a text search case insensitive with regex query with spring-data mongo . For example in Oracle : select * from user where lower(username) like '%ab%' How can i make this query with spring-data mongo ? Thanks in advance 回答1: You can try something like below. Assumes you have a User pojo class. Using MongoTemplate i option for case insensitive: Criteria regex = Criteria.where("username").regex(".*ab.*", "i"); mongoOperations.find(new Query().addCriteria(regex), User.class); Using

Object type in mongoose

北城余情 提交于 2020-04-07 22:13:49
问题 I am defining a mongoose schema and definition is as follows: inventoryDetails: { type: Object, required: true }, isActive:{ type:Boolean, default:false } I tried "Object" type and I am seeing my data is getting saved successfully. When I changed type to array, the save is failing. Sample Data: { "inventoryDetails" : { "config" : { "count" : { "static" : { "value" : "123" }, "dataSource" : "STATIC" }, "title" : { "static" : { "value" : "tik" }, "dataSource" : "STATIC" } }, "type" : "s-card

Add an element to an array, if it exists don't add it, if it exists update it

点点圈 提交于 2020-04-07 07:09:09
问题 I am making a voting system in which an object will have an array called scores , every time it is qualified a record will be entered to this array as long as the same id does not exist. Sample Doc : { name:"jose luis", scores:[] } The user push : {id:1,stars:5} Updated Doc : { name:"jose luis", scores:[{id:1,stars:5}] } The user push 2nd time : {id:1,stars:4} Updated Doc : { name:"jose luis", scores:[{id:1,stars:4}] } //this should update the value of the stars under the array element with

Add an element to an array, if it exists don't add it, if it exists update it

北慕城南 提交于 2020-04-07 07:08:49
问题 I am making a voting system in which an object will have an array called scores , every time it is qualified a record will be entered to this array as long as the same id does not exist. Sample Doc : { name:"jose luis", scores:[] } The user push : {id:1,stars:5} Updated Doc : { name:"jose luis", scores:[{id:1,stars:5}] } The user push 2nd time : {id:1,stars:4} Updated Doc : { name:"jose luis", scores:[{id:1,stars:4}] } //this should update the value of the stars under the array element with