在MongoDB中,我有两个新的数据集合,它们是我从一个旧的Firestore数据库中提取的,我要搬到mongo。由于这两个集合之间的总数大约为20,000,所以我选择将原始JSON粘贴到mongo中的insert部分,该部分工作起来很有魅力,而且我也不必编写一个新的插入路径。
然后,我在Mongoose中创建了一个与插入的文档匹配的模式,并尝试使用该模式来提取一些数据,并且它总是不返回任何数据。
通过JSON插入的票证示例:
{
"title": "How to add and manage users for your company in QuickBooks Online",
"priority": "1",
"type": "Video",
"course": "G205",
"transcriptId": "07dom27Zz98jakvB1oh5",
"status": "In Review",
"tags": "",
"url": "",
"revisionNumber": 0,
"directoryId": 19,
"checkedOut": false
},
和我做的匹配的模式。mongo中的集合名也称为oldTickets,这里是我的模式名的复数:
const mongoose = require('mongoose');
var Schema = mongoose.Schema
const schema = new Schema({
course: { type: String },
title: { type: String },
priority: { type: String },
type: { type: String },
course: { type: String },
transcriptId: { type: String },
status: { type: String },
tags: { type: String },
url: { type: String },
revisionNumber: { type: Number },
directoryId: { type: Number },
checkedOut: { type: Boolean },
});
module.exports = mongoose.model('oldTicket', schema);
最后,我的模型导入并获取调用:
const OldTicket = require('./models/model_old_ticket');
/***************************************************************************
* Get Old Tickets - Returns all old tickets, 10 at a time
****************************************************************************/
app.get('/getOldTickets/:offset', (req, res) => {
checkConnection();
OldTicket.find().skip(parseInt(req.params.offset)).limit(10).exec((err, data) => {
if (err){ res.status(500).send({err: err}); }
//If we got data, count the tickets & return the tickets & count
if (data) {
OldTicket.find().countDocuments().then(count => {
return res.status(200).send({
tickets: data,
count: count
})
})
}
});
});
为什么这个发现什么都没有?数和票都是0。在没有模式的情况下手动创建集合时,我曾遇到过这个问题,在这些情况下,我只需删除集合,编写创建文档的路由,然后一切就会正常工作。但是,由于这两个集合的数据量很大,我宁愿不这样做,因为一切都应该按原样工作。
编辑:蒙古文文档示例
以及我目前正在查看的集合的名称:
我刚刚意识到,由于某种原因,现在有两个集合名,oldTickets,它有数据,和old票证,它是空的。我假设我的查询是在空查询中搜索的?我怎样才能把它转到真正有数据的那个呢?
发布于 2022-03-10 05:11:36
你能把你的数据的截图附在集合里吗?可能是different.in猫鼬,每个集合名都有's‘。请验证您的集合是由您手工创建的,然后它必须与猫鼬模式相同,并且还可以使用's‘完成。示例:
const mongoose = require("mongoose");
const schema = new mongoose.Schema(
{
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
index: true
},
filmId: {
type: mongoose.Schema.Types.ObjectId,
index: true
},
filmType: {
type: String,
index: true
},
birthday: {
type: Date
},
age: {
type: Number
},
terms: {
type: Boolean
}
},
{
versionKey: false,
timestamps: true,
}
);
schema.index({ filmId: 1, user: 1 })
module.exports = mongoose.model("UserAgeVerification", schema);
请看我的数据库
https://stackoverflow.com/questions/71416584
复制相似问题