我正在尝试使用一个查询来搜索我的数据库,查找当前有特定项目的字符,然后使用console.log查找字符。
这些项目实际上是参考模型。
例如:
字符
const reducers = combineReducers({
geod:reducer
});
function configureStore(initialState = {}) {
const store = createStore(
reducers,
initialState,
applyMiddleware(thunk)
)
return store;
};
var ping = function ping(store) {
return function (next) {
return function (action) {
//EX1
/*
if(action.type !== 'ACTIVATE_GEOD') return next(action)
console.log(`Тип события: ${action.type}, события: ${action}`)
return next(action);
*/
//EX2
console.log(`Тип события: ${action.type}, события: ${action}`)
return next(action);
};
};
};
window.store = configureStore();
store.subscribe(() => console.log(store.getState()))
function logout(){
return function(dispatch){
setTimeout(function(){
activateGeod({title:'6 sec'})
},6000)
}
}
// App.js
class App extends React.Component {
render() {
return (
<div>
PAGE:
<h1>{this.props.geod.title || 'Hello World!'}</h1>
{this.props.geod.title ?
<button onClick={this.props.closeGeod}>
Exit Geod
</button> :
<button onClick={() =>
this.props.activateGeod({ title: 'I am a geo dude!' })}>
Click Me!
</button>
}
<button onClick={() => this.props.dispatch( logout() ) }>
THUNK REDUX Click Me!
</button>
</div>
);
}
}
// AppContainer.js
const mapStateToProps = (state, ownProps) => ({
geod: state.geod
});
/*
const mapDispatchToProps = {
activateGeod,
closeGeod,
};
*/
const mapDispatchToProps = (dispatch) => {
return {
activateGeod: bindActionCreators(activateGeod, dispatch),
closeGeod: bindActionCreators(closeGeod, dispatch),
}
};
const AppContainer = connect(
mapStateToProps,
mapDispatchToProps
)(App);
ReactDom.render(
<Provider store={store}>
<AppContainer />
</Provider>,
document.getElementById('wrapper')
);
项目
var characterSchema = new mongoose.Schema({
name: String,
items: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Item"
}
]
});
module.exports = mongoose.model("Character", characterSchema);
到目前为止,我已经尝试过这个:
var itemSchema = new mongoose.Schema({
name: String,
owners: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Character"
}
]
});
module.exports = mongoose.model("Item", itemSchema);
我认为这样做不会起作用&#34; item&#34;字符内只是一个引用项目的数组,但我不知道从哪里开始..
答案 0 :(得分:0)
您需要使用聚合来加入引用的文档,然后搜索它们。
Character.aggregate([
// deconstruct array field (as you can't join on an array field)
{
$unwind: '$items'
},
// join items collection
{
$lookup: {
from: 'items', // collection to join
localField: 'items', // field from the input documents
foreignField: '_id', // field from the documents of the "from" collection
as: 'item' // output array field
}
},
// deconstruct array field
{
$unwind: '$item'
},
// filter
{
$match: {
'item.name': 'Greatsword'
}
},
// rebuild
{
_id: '_id',
name: { $first: '$name' },
items: { $push: '$item' }
}
], function (err, characters) {
});
有关详细信息,请阅读