mongodb

MongoDB - Update field in an array object based on nested array's field value

喜夏-厌秋 提交于 2021-02-05 08:13:28
问题 I am trying to update a field inside array of objects, where field in nested array is equal to a value. My goal here is to set the picture field a new url, where value field in valueList is oldRed Product schema: { variations: [{ id: 1, picture: 'https://example.picture.com', valueList: [{ name: 'color', value: 'oldRed' }, { name: 'size', value: 'M' }] }, { id: 2, picture: 'https://example.picture.com', valueList: [{ name: 'color', value: 'black' }, { name: 'size', value: 'M' }] }] } The

MongoDB Aggregation seems very slow

白昼怎懂夜的黑 提交于 2021-02-05 08:09:56
问题 I have a mongodb instance running with the following stats: { "db" : "s", "collections" : 4, "objects" : 1.23932e+008, "avgObjSize" : 239.9999891553412400, "dataSize" : 29743673136.0000000000000000, "storageSize" : 32916655936.0000000000000000, "numExtents" : 39, "indexes" : 3, "indexSize" : 7737839984.0000000000000000, "fileSize" : 45009076224.0000000000000000, "nsSizeMB" : 16, "dataFileVersion" : { "major" : 4, "minor" : 5 }, "extentFreeList" : { "num" : 0, "totalSize" : 0 }, "ok" : 1

How to clone a collection from one MongoDB to another on same server

懵懂的女人 提交于 2021-02-05 08:01:45
问题 I'm using Mongo 3.2. I have two databases on my localhost named client1 and client2 . Now client1 contains a collection named users . I want to clone this collection to client2 . I have tried:- use client2 db.cloneCollection('localhost:27017', 'client1.users', { 'active' : true } ) This outputs { "ok" : 0.0, "errmsg" : "can't cloneCollection from self" } Is cloning a collection from one db to another on the same server prohibited? 回答1: Few things : In general cloneCollection is used for

How to fetch data from MongoDB collection in C# using Regular Expression?

穿精又带淫゛_ 提交于 2021-02-05 08:00:31
问题 I am using MongoDB.Drivers nuget package in my MVC (C#) web application to communication with MongoDB database. Now, I want to fetch data based on specific column and it's value. I used below code to fetch data. var findValue = "John"; var clientTest1 = new MongoClient("mongodb://localhost:XXXXX"); var dbTest1 = clientTest1.GetDatabase("Temp_DB"); var empCollection = dbTest1.GetCollection<Employee>("Employee"); var builder1 = Builders<Employee>.Filter; var filter1 = builder1.Empty; var

Mongoose - find(): object inside search options is not working

耗尽温柔 提交于 2021-02-05 07:52:06
问题 I have a mongoose Schema that looks like this: var mySchema = new mongoose.Schema({ ... metadata: { isDeleted: { type: Boolean, default: false }, ... } }); I want to get the list of elements in my mongodb database applying a filter, so I have the following object: var searchOptions = { metadata: { isDeleted: false } }; that always needs to have that metadata.isDeleted value set to false , appart from other parameters that will be added later, before executing the query: var objQuery = myModel

Mongo updating inside a double nested array

一世执手 提交于 2021-02-05 07:48:29
问题 I have a mongo collection that looks like this db.users.find().pretty() { "_id" : ObjectId("57c3d5b3d364e624b4470dfb"), "fullname" : "tim", "username" : "tim", "email" : "tim@gmail.com", "password" : "$2a$10$.Z9CnK4oKrC/CujDKxT6YutohQkHbAANUoAHTXQp.73KfYWrm5dY2", "workout" : [ { "workoutId" : "Bkb6HIWs", "workoutname" : "chest day", "BodyTarget" : "Chest", "date" : "Monday, August 29th, 2016, 2:27:04 AM", "exercises" : [ { "exerciseId" : "Bym88LZi", "exercise" : "bench press", "date" :

Group and count using aggregation framework

寵の児 提交于 2021-02-05 07:48:07
问题 I'm trying to group and count the following structure: [{ "_id" : ObjectId("5479c4793815a1f417f537a0"), "status" : "canceled", "date" : ISODate("2014-11-29T00:00:00.000Z"), "offset" : 30, "devices" : [ { "name" : "Mouse", "cost" : 150, }, { "name" : "Keyboard", "cost" : 200, } ], }, { "_id" : ObjectId("5479c4793815a1f417d557a0"), "status" : "done", "date" : ISODate("2014-10-20T00:00:00.000Z"), "offset" : 30, "devices" : [ { "name" : "LCD", "cost" : 150, }, { "name" : "Keyboard", "cost" : 200,

Update array inside of Mongo document doesn't work

自作多情 提交于 2021-02-05 07:32:42
问题 I have a user's collection, each user has a subscription array of objects. I want to add another subscription to that array. I'm getting the new subscription, updating it with findByIdAndUpdate , but it doesn't add the new subscription, however it shows that the document was updated. I tried several approaches but nothing worked well. Here is the last approach: ... const { user_id } = req.params; const subscription = req.body; // Getting subscription const user = await UserModel.findById(user

Update array inside of Mongo document doesn't work

非 Y 不嫁゛ 提交于 2021-02-05 07:32:08
问题 I have a user's collection, each user has a subscription array of objects. I want to add another subscription to that array. I'm getting the new subscription, updating it with findByIdAndUpdate , but it doesn't add the new subscription, however it shows that the document was updated. I tried several approaches but nothing worked well. Here is the last approach: ... const { user_id } = req.params; const subscription = req.body; // Getting subscription const user = await UserModel.findById(user

Wrong count of documents in MongoDB shard cluster

安稳与你 提交于 2021-02-05 06:56:46
问题 I have a cluster with three shards using MongoDB 4.2. I have a collection (users) that, before sharding can be checked it has 600000 documents: mongos> db.users.count() 600000 Next, I shard it with the usual commands (first DB, next collection): mongos> sh.enableSharding("app") mongos> sh.shardCollection("app.users", {"name.first": 1}) getting after a couple of minutes or so an equally distrution of chunks amongs the shards: chunks: shard0000 3 shard0001 2 shard0002 3 So far so good. However,