I'm quite new to mongodb, hence I would like to know how the mongoose schema should look like when I need to add indexed items in an array.
Here is how I want the output to look like:
_id: some_id
users: Object
0: Array
0: some_user_id
1: some_user_id
2: some_user_id
This is the schema I tried to create, but I think I'm wrong here:
const mongoose = require('mongoose')
const timerSchema = mongoose.Schema({
users: [[]]
})
module.exports = timerSchema
Thank you in advance!
If you want timerSchema.users property to hold an array of arrays then your schema definition is correct.you can also specify type of array, refer This example to create array of arrays using mongoose Schema which contain output as well.
or if you want users as object then,
const timerSchema = mongoose.Schema({
users: {
<Field1>:[[]],
<Field2>:<Type>
}
})
Related
I'm not sure if this is intentional or not, but I cannot seem to make defaults work for aggregate(). For example, if I add a new field to the schema:
const userSchema = new mongoose.Schema({ sessionIds: { type: [String], default: [] } });
export.User = mongoose.model('User', userSchema);
and run the query on existing documents (which don't have this field yet)
const users = await User.find({}).exec();
console.log(users[0].sessionIds);
This will print an empty array correctly. However, when I run the same query, but using aggregate, it prints undefined.
const users = await User.aggregate({ $match: {} }).exec();
console.log(users[0].sessionIds);
Is there a way I can tell Mongoose to apply the default values for the aggregation or apply it to the results after the aggregation ran?
Edit: After experimenting a bit more, I figured out that it has to do something with aggregate returning lean results, while find returning Mongoose objects. Is there a way to make aggregate return mongoose objects as well?
My code below is returning NULL immediately when it should return a document, seems to be specific to this collection as my other findOne's for different collections are working as expected.
function findData(rowObj) {
console.log(rowObj.field);
console.log(Mongoose.connection.readyState);
return DataImport.findOne({
ROW: rowObj.field
});
}
My collection has many fields, ideally I wouldn't use a schema as I am not updating or adding from this collection, just reading from it;
const mongoose = require('mongoose');
const { Schema } = mongoose;
const dataImportSchema = new Schema({});
const DataImport = mongoose.model(
'DataImport',
dataImportSchema,
'DataImport'
);
module.exports = DataImport;
I have however tried this with all of the document fields in the schema but get the same result.
Is it possible an issue with the collection being too large? It is 30GB with around 40 million documents.
I seem to have been using an old way of specifying the collection name and should have had it in the schema definition: https://mongoosejs.com/docs/guide.html#collection
`
When the table name includes a capital letter (e.g. fooBar), Moongoose Model.find() returns an empty array. If I change both the table name and the search string to be lowercase without changing anything else - it works fine.
Is this expected?
I think I have read somewhere about collection names being treated by default as lowercase inside of mongoose. I always circumvent it by using models:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const FoobarSchema = new Schema({
name: { type: String }
}, { collection: 'fooBar' });
module.exports = mongoose.model('fooBar', FoobarSchema);
The important part is what you name the collection in the Schema definition. In the export that 'fooBar' can be named whatever you want and is a means to reference the model in your code.
I’m looking for the fastest way to get all objectIDs of a collection with a privacy value 'public'.
In this image, privacy's value is 'public', so node should give me the '_id' of this object (in this example '57bc4b9f466fab7c099a3f94').
My attempt:
var mongoose = require('mongoose');
mongoose.connect('localhost:27017/databasename');
var Schema = mongoose.Schema;
var collectionsNameSchema = new Schema({
updated: {type: Date },
privacy: { type: Object }
}, {collection: 'spots'});
var collectionsNameData = mongoose.model('collectionsNameData', collectionsNameSchema);
...
collectionsNameData.find({privacy: 'public'})
From what i see you have a problem in query to mongoDB.
Try like this.
collectionsNameData.find({'privacy.value': 'public'});
This should return desired result.
You also may want to use projection as second parameter in find to return only fields that you want. Keep in mind that _id returned by default.
Hope this helps.
I'm calling MongoDB from my Node app using Mongoose like this:
var query = itemModel.find();
query.exec(function (err, items) {
console.log(err);
socket.emit("items", items);
});
I have 3 models defined like this:
var Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;
var playerModel = require('./models/player.js').make(Schema, mongoose);
var characterModel = require('./models/character.js').make(Schema, mongoose, ObjectId);
var itemModel = require('./models/item.js').make(Schema, mongoose);
my models look like this:
function make(Schema, mongoose) {
itemSchema = new Schema({
name: String
, bonus: [{
type: String
, value: Number
}]
, price: Number
, slot: String
});
return mongoose.model('Character', characterSchema);
}
exports.make = make;
For some reason I'm getting all documents, regardless of them being items, characters or players. Since I'm calling find() on itemModel I was expecting only Items, what am I doing wrong?
The model that you have shown appears to be the item model, but you are creating the model with the 'Character' name. This means that you told Mongoose about the scheme for an item and that it is stored in the 'character' collection. Assuming you've done the same for each other model (Character/Player), you've been Mongoose that everything is in the same collection.
Then you query that collection and you seem surprised that everything is stored in the same collection. It strikes me as if you have little experience with Mongoose/MongoDB, so I will suggest you download and learn to love MongoVUE. This application is a good GUI to see what is going on under the hood of the MongoDB database. While developing, you also might want to enable debugging so you can see what queries mongoose is launching to the server (mongoose.set('debug', true)).