mongodb

$lookup when foreignField is array

爷,独闯天下 提交于 2021-02-10 14:41:03
问题 I have 2 collections, the first storing the animes watched by each user, their status etc: const listSchema = mongoose.Schema({ user: { type: Schema.Types.ObjectId, ref: 'user' }, animes: [{ _id: false, anime: { type: Schema.Types.ObjectId, ref: 'anime', unique: true }, status: String, episodes: Number, rating: Number }] }); and the second storing all animes and the relevant information. I want to show that second collection but adding status and episodes fields that are filled from the list

$lookup when foreignField is array

守給你的承諾、 提交于 2021-02-10 14:40:49
问题 I have 2 collections, the first storing the animes watched by each user, their status etc: const listSchema = mongoose.Schema({ user: { type: Schema.Types.ObjectId, ref: 'user' }, animes: [{ _id: false, anime: { type: Schema.Types.ObjectId, ref: 'anime', unique: true }, status: String, episodes: Number, rating: Number }] }); and the second storing all animes and the relevant information. I want to show that second collection but adding status and episodes fields that are filled from the list

Need to push data in nested subdocument array

心不动则不痛 提交于 2021-02-10 14:37:53
问题 I need to push data in nested subdocument array(replyComment): This is an example of a document from my database: { comments: [ { replyComment: [], _id: 601a673735644c83e0aa1be3, username: 'xyz123@gmail.com', email: 'xyz213@gmail.com', comment: 'test123' }, { replyComment: [], _id: 601a6c94d1653c618c75ceae, username: 'xyz123@gmail.com', email: 'xyz123@gmail.com', comment: 'reply test' } ], _id: 601a3b8038b13e70405cf9ea, title: 'latest test', snippet: 'latest test snippet', body: 'latest test

How rename a mongodb database

若如初见. 提交于 2021-02-10 13:15:37
问题 I need to rename a MongoDB DB database programmatically. I have not seen a way to do this with the MongoDB c# driver. I would like to do something like: this.mongoClient.renameDatabase("oldName","newName"); I figure I can roll my own but I feel like this should be possible with a current driver. 回答1: run a renameCollection command on the admin database for every collection name in your source db with the format: renameCollection: 'old.CollectionName', to: 'new.CollectionName' here's how to do

How rename a mongodb database

元气小坏坏 提交于 2021-02-10 13:12:04
问题 I need to rename a MongoDB DB database programmatically. I have not seen a way to do this with the MongoDB c# driver. I would like to do something like: this.mongoClient.renameDatabase("oldName","newName"); I figure I can roll my own but I feel like this should be possible with a current driver. 回答1: run a renameCollection command on the admin database for every collection name in your source db with the format: renameCollection: 'old.CollectionName', to: 'new.CollectionName' here's how to do

MongoDB: Check if value is null or the array is empty

痞子三分冷 提交于 2021-02-10 13:09:29
问题 I would like to match all documents that don't contain the "Company" attribute or where the "Company" value is null or an empty array. User.find({Company: {$in: [null, [] ]}}, function (err, users) { if (err) { throw err; } console.log(users.length); }).then(function(doc) { console.log("finish User Company"); }); 回答1: You could use the $or query operator with a check for each condition: { $or: [{ // Check about no Company key Company: { $exists: false } }, { // Check for null Company: null },

Sort by date string (ascending) on Mongo

試著忘記壹切 提交于 2021-02-10 13:05:09
问题 I have a database with a date column in the format dd/mm/yyyy and I'd like to sort by the date in ascending order. $cursor = $collection->find($filter)->sort(array('date' => 1, 'tripID' => 1)); The date is a string and I'm also filtering on the tripID but that aspect is working. The problem is that at present I'm getting: 01/01/2014 01/02/2014 02/01/2014 02/02/2014 What I would like is: 01/01/2014 02/01/2014 01/02/2014 02/02/2014 Is it possible to achieve this using the query or would it need

MongoDB $pull success indicator with mgo Golang

三世轮回 提交于 2021-02-10 12:49:05
问题 I am currently using the $pull operator successfully to delete an item in an array. The Data structure looks like this { _id: 234, comments: [{id:1},{id:2},{id:3}] } My change struct change := mgo.Change{ Update: bson.M{ "$pull": bson.M{ "comments": bson.M{ "id": c.Id, }, }, }, ReturnNew: true, } and my mongo command test, err := cs.Find(bson.M{"_id": 234}).Apply(change, nil) In the mongo shell, simulating the same command would return useful meta information On success WriteResult({

Getting a CastError NaN for a Number

痞子三分冷 提交于 2021-02-10 12:27:09
问题 I get this error { [CastError: Cast to Number failed for value "NaN" at path "area"] message: 'Cast to Number failed for value "NaN" at path "area"', name: 'CastError', kind: 'Number', value: NaN, path: 'area' } } } for this code MySchema.methods = { updateArea: function (oldArea, newArea) { var self = this; self.area = self.area - oldArea + newArea; return self.save(function (err) { if (err) { console.log(err); return err; } }); } }; However, when I logged the typeof self.area, it gives me

Two Way Embedding vs. One Way Embedding in MongoDB (Many-To-Many)

守給你的承諾、 提交于 2021-02-10 12:13:27
问题 I would like to discuss with you, if it makes any sense to use two way embedding instead of one way embedding, when modeling an N:M relationship in MongoDB. Let's say, we have two entities: A Product can belong to many (few) Categories , and a Category can have many (lots of) Products . Two Way Embedding If we use this approach, our categories would look like this: { _id: 1, name: "Baby", products: [2] } { _id: 2, name: "Electronics", products: [1, 2] } And products : { _id: 1, name: "HDMI