mongodb-query

Dynamic keys after $group by

。_饼干妹妹 提交于 2019-12-18 09:25:54
问题 I have following collection { "_id" : ObjectId("5b18d14cbc83fd271b6a157c"), "status" : "pending", "description" : "You have to complete the challenge...", } { "_id" : ObjectId("5b18d31a27a37696ec8b5773"), "status" : "completed", "description" : "completed...", } { "_id" : ObjectId("5b18d31a27a37696ec8b5775"), "status" : "pending", "description" : "pending...", } { "_id" : ObjectId("5b18d31a27a37696ec8b5776"), "status" : "inProgress", "description" : "inProgress...", } I need to group by

MongoDB lookup when foreign field is an array of objects

六月ゝ 毕业季﹏ 提交于 2019-12-18 09:14:56
问题 I have two collections initiatives and resources : initiative document example: { "_id" : ObjectId("5b101caddcab7850a4ba32eb"), "name" : "AI4CSR", "ressources" : [ { "function" : ObjectId("5c3ddf072430c46dacd75dbb"), "participating" : 0.1, }, { "function" : ObjectId("5c3ddf072430c46dacd75dbc"), "participating" : 5, }, { "function" : ObjectId("5c3ddf072430c46dacd75dbb"), "participating" : 12, }, { "function" : ObjectId("5c3ddf072430c46dacd75dbd"), "participating" : 2, }, ], } and a resource

Select based on timestamp and update timestamp with zero

你说的曾经没有我的故事 提交于 2019-12-18 07:15:18
问题 How do I select records from a date field which has a time (HH:MM:SS.Milisecond) value greater than zero from a MongoDB collection and update it with a time (HH:MM:SS) value as zero by keeping the date value the same as the existing value in a Python script? The current data would look like as below - 1) "createdDate" : ISODate("2015-10-10T00:00:00Z") 2) "createdDate" : ISODate("2015-10-11T00:00:00Z") 3) "createdDate" : ISODate("2015-10-12T00:00:00Z") 4) "createdDate" : ISODate("2015-10-13T01

Select based on timestamp and update timestamp with zero

為{幸葍}努か 提交于 2019-12-18 07:15:01
问题 How do I select records from a date field which has a time (HH:MM:SS.Milisecond) value greater than zero from a MongoDB collection and update it with a time (HH:MM:SS) value as zero by keeping the date value the same as the existing value in a Python script? The current data would look like as below - 1) "createdDate" : ISODate("2015-10-10T00:00:00Z") 2) "createdDate" : ISODate("2015-10-11T00:00:00Z") 3) "createdDate" : ISODate("2015-10-12T00:00:00Z") 4) "createdDate" : ISODate("2015-10-13T01

Sort Documents Without Existing Field to End of Results

∥☆過路亽.° 提交于 2019-12-18 06:58:50
问题 I have the following documents, few documents only have bids fields. Collection: { "_id" : "PqwSsLb2jsqTycMWR", "name" : "aaa", "bids" : [ { "amount" : NumberInt(450) } ] } { "_id" : "93EDoQfeYEFk8pyzX", "name" : "bbb" } { "_id" : "j5wkK5Eagnwuo8Jym", "name" : "ccc", "bids" : [ { "amount" : NumberInt(520) } ] } { "_id" : "eLaTyM5h5kqA97WQQ", "name" : "ddd" } If I sort with bids.amount : 1 am getting below results Result: { "_id" : "93EDoQfeYEFk8pyzX", "name" : "bbb" } { "_id" :

Mongodb setting unique field

放肆的年华 提交于 2019-12-18 06:48:30
问题 TENANT { "_ID" : 11, NAME : "ruben", OPERATION :[{OPERATION_ID: 100, NAME : "Check"}] } how to set the OPERATION_ID has unique to avoid duplicate values and to avoid null values like primary key? 回答1: When you want the OPERATION_IDs to be unique for all tenants, then you can do it like that: db.tenants.ensureIndex( { operation.OPERATION_ID : 1 }, { unique:true, sparse:true } ); When you want the OPERATION_IDs to be unique per tenant, so that two tenants can both have the operation_ID:100 but

Mongodb setting unique field

眉间皱痕 提交于 2019-12-18 06:48:05
问题 TENANT { "_ID" : 11, NAME : "ruben", OPERATION :[{OPERATION_ID: 100, NAME : "Check"}] } how to set the OPERATION_ID has unique to avoid duplicate values and to avoid null values like primary key? 回答1: When you want the OPERATION_IDs to be unique for all tenants, then you can do it like that: db.tenants.ensureIndex( { operation.OPERATION_ID : 1 }, { unique:true, sparse:true } ); When you want the OPERATION_IDs to be unique per tenant, so that two tenants can both have the operation_ID:100 but

Mongodb setting unique field

半城伤御伤魂 提交于 2019-12-18 06:48:02
问题 TENANT { "_ID" : 11, NAME : "ruben", OPERATION :[{OPERATION_ID: 100, NAME : "Check"}] } how to set the OPERATION_ID has unique to avoid duplicate values and to avoid null values like primary key? 回答1: When you want the OPERATION_IDs to be unique for all tenants, then you can do it like that: db.tenants.ensureIndex( { operation.OPERATION_ID : 1 }, { unique:true, sparse:true } ); When you want the OPERATION_IDs to be unique per tenant, so that two tenants can both have the operation_ID:100 but

Mongo - How to update multiple documents with different value in single query?

守給你的承諾、 提交于 2019-12-18 06:31:19
问题 I want to write a query for updating multiple documents in a single query, Please suggest me possible ways. Following is my mongo document. [ { "_id" : ObjectId("5b0f0a2ca1f6633032c204cd"), "parent_id" : ObjectId("5b0f09e1a1f6633032c204cc"), "name" : "ABC", "userType": "admin" }, { "_id" : ObjectId("5b0f0a2ca1f6633032c204ce"), "parent_id" : ObjectId("5b0f09e1a1f6633032c204cc"), "name" : "DEF", "userType": "admin" }, { "_id" : ObjectId("5b0f0a2ca1f6633032c204cf"), "parent_id" : ObjectId(

MongoDB query for document older than 30 seconds

别等时光非礼了梦想. 提交于 2019-12-18 05:05:34
问题 Does anyone have a good approach for a query against a collection for documents that are older than 30 seconds. I'm creating a cleanup worker that marks items as failed after they have been in a specific state for more than 30 seconds. Not that it matters, but I'm using mongojs for this one. Every document has a created time associated with it. 回答1: We are assuming you have a created_at or similar field in your document that has the time it was inserted or otherwise modified depending on