Views are not working (populating, or triggering) properly with $unionWith #967
-
Versions
package: mongo-memory-server What is the Problem?Querying View (for all content) returns 0 documents, while individual content collections have content. Code Exampleasync function bootstrapContentView() {
const viewName = 'contents';
const sourceCollection = 'articles';
const viewPipeline = [
{
$addFields: {
recompletable: true
}
},
{
$unionWith: {
coll: "media",
pipeline: [
{
$addFields: {
recompletable: true
}
}
]
}
},
{
$unionWith: {
coll: "importedcontents",
pipeline: [
{
$addFields: {
recompletable: false
}
}
]
}
}
];
const db = mongoose.connection.db;
const existing = await db.listCollections({ name: viewName }, { nameOnly: true }).toArray();
if (existing.length > 0) {
try {
log.info(`View ${ viewName } already exists.`);
const updateResult = await db.command({ collMod: viewName, viewOn: sourceCollection, pipeline: viewPipeline });
log.info(`View ${ viewName } updated with result: ${ JSON.stringify(updateResult) }`);
return;
} catch (error) {
// Handle other potential errors
log.error(error, `Error bootstrapping updating view: ${ error.message }`);
}
}
try {
const createResult = await db.command({ create: viewName, viewOn: sourceCollection, pipeline: viewPipeline });
log.info(`View ${ viewName } created with result: ${ JSON.stringify(createResult) }`);
return;
} catch (error) {
if (error.codeName === 'NamespaceExists' || error.code === 48 || error.code === 59) {
log.info(`View ${ viewName } already exists. `);
} else {
// Handle other potential errors
log.error(error, `Error bootstrapping view: ${ error.message }`);
}
}
}
export async function connectToDB() {
// followed some recommendations from:
// -- https://gist.github.com/mongolab-org/9959376
// -- http://mongoosejs.com/docs/connections.html
// -- http://mongodb.github.io/node-mongodb-native/2.1/api/Server.html
// -- http://mongodb.github.io/node-mongodb-native/2.1/api/ReplSet.html
const options = {
maxPoolSize: config.db.maxConnectionPoolSize, // per container connection limit. We currently have around a 500 total connection limit on the mongo server side
connectTimeoutMS: 30000,
family: 4
};
if (config.testing && config.containerized === false) {
if (!mongoMemoryDb) {
log.info('Configuring Mongo Memory Server');
/* eslint-disable-next-line global-require */
const { MongoMemoryReplSet } = require('mongodb-memory-server');
mongoMemoryDb = await MongoMemoryReplSet.create({
binary: { downloadDir: 'node_modules/.cache/mongodb-memory-server/mongodb-binaries' },
count: 1,
});
// only override url if we're not running tests inside a docker container. If we are, tests
// will connect to a mongo container instead of mongo memory server
config.db.connectionUrl = mongoMemoryDb.getUri('TestingDB');
}
}
try {
const connection = await mongoose.connect(config.db.connectionUrl, options);
log.info(`mongoose.connect Called Successfully. ${ config.db.connectionUrl }`);
await bootstrapContentView();
return connection;
} catch (err) {
log.error(err, 'Error connecting to MongoDB');
throw err;
}
}
// Filtered query returns 0 documents
// const contents = await AllContent.find({_id: {$in: contentIds}}, '_id type recompletable').lean().exec();
// console.log('contents to check for recompletion', contents);
// $ contents to check for recompletion []
// Unfiltered query returns 0 documents
// const cont = await AllContent.find({}, '_id type recompletable').limit().lean().exec();
// console.log('all content no filter', cont);
// $ all content no filter []NOT in the code above but if you do the native driver call you also get 0 documents: Note some items have been renamed. Do you know why it happens?no -------- added 2026-02-14 8:42 AM EST -------------- |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
|
You are using MMS 11.x, and dont define a binary version, so i assume the default for that version This does not look to be actually MMS related and I have not used Due to that, i will move this to be a discussion. (though you are likely better off asking on stackoverflow) |
Beta Was this translation helpful? Give feedback.
-
{ autoCreate: false, autoIndex: false, autoSearchIndex: false, collection: 'contents' }Also double check with a drop statement if found. const existing = await db.listCollections({ name: 'contents' }, { nameOnly: true }).toArray();
if (existing.length > 0) {
try {
log.info(`View contents was found. Attempting to drop it and create it with the latest pipeline.`);
const dropCollectionResult = await mongoose.connection.dropCollection(ContentsReadViewName);
log.info(`View contents dropped with result`, dropCollectionResult);
} catch (error) {
// Handle other potential errors
log.error(error, `Error bootstrapping: dropping view/collection: ${ error.message }`);
throw error;
}
} |
Beta Was this translation helpful? Give feedback.
Also double check with a drop statement if found.